-4

I have a question ovi, my question is about input() how do I make a input on a if statement block and make it about it for example a heads and tails game.

New contributor
GUY RUFF is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
1
  • Please provide enough code so others can better understand or reproduce the problem. Commented yesterday

2 Answers 2

0

You can store the text a person has entered in a variable and then check the contents of it or directly use input() inside an if statement, though not advised.

This snippet will take an input and check if it contains "hello", then print a line depending on what has been entered by the user.

if "hello" in input("Enter something: "):
    print("Hi, what's up?")
else:
    print("Wanna play a game?")
print("Goodbye")

If the user enters "hello world", the program will first output "Hi, what's up" and then "Goodbye".

As mentioned, using input() inside an if statement is not advised, as it makes it less readable. Here is how my example would look like using variables:

txt = input("Enter something: ") # the variable txt now equals what the user has entered
if "hello" in txt: # We can now use txt in place of the input statement
    print("Hi, what's up?")
else:
    print("Wanna play a game?")
print("Goodbye")

If you would want to make a heads and tails game, in which the user has to guess the outcome of a coin throw you will need to combine the usage of input, and checking what the user has entered, together with a random outcome. You can use the random library for that. Here is a quick example

import random # We need this line to use the library random, which has functions that we need

numbers = ["1","2,"3"]

print("Welcome to number guessing.")
print("I am thinking of a random number between 1 and 3, can you guess it?")
solution = random.choice(numbers) # Pick a random number from the array
guess = input("Enter a guess: ")
if guess == solution:
    print("Good job!")
else:
    print("That's not it...")

Normally you would use random.randint to generate random numbers, but in this example I used random.choice to make it easier for you to get to building your heads and tails game. random.choice chooses a random object from the array. We then check if the number entered by the user is the same as the randomly guessed one. If it is, we print out a winning line. If not, we go into the else statement.

You would now have to think: What are the options in a heads and tails game, how can I "simulate" the coin throw by randomly choosing an outcome and how can I check whether the user has correctly guessed the outcome?

Feel free to leave your solution as an edit

Sign up to request clarification or add additional context in comments.

Comments

-1

input() takes input from the console (stdin), waits until Enter key is pressed and returns the input, which you can store it in a variable. You can pass a string as an argument and it will print it out before waiting for input.

New contributor
oulol is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.