Skip to content
Sign upLog in
← Back to Community

How To Use "if input = whatever" statements in python

Profile icon
ffsgivemeaname

so i started a few days ago and i cant find anything on how to use if statements other than with numbers, and just to clarify im trying to use it in a text adventure stance for my dads birthday (hes a coder)

Answered by MocaCDeveloper [earned 5 cycles]
View Answer
Voters
Profile icon
LunarIvypool
Profile icon
ffsgivemeaname
Comments
hotnewtop
Profile icon
MocaCDeveloper

Simple!

phrase = input('Put a phrase: ') if phrase == "hey": print("HI!")

That is just a simple example. But that would be how you do it.
Using the input built-in function in Python, you can put any string inside of it and it will print it out then expect input afterwards.
The value of the input is then stored in the variable name. You can then use the variable name to check the value!

Profile icon
RYANTADIPARTHI

You use it in this way:

example = input("what ? ") if example == "Whatever": # checks print("ok") elif example == "Something": print("ok..") else: print("ok.....")

You can use as many elifs as you want

Profile icon
Bookie0

Hi,

so here's a basic program that determines if what the user says is equal to something:

name = input("Enter your name! ") # asks user for their name # conditionals if name == "Bob": # if what the user wrote equals "Bob" print("Hi bob!") # remember indentation elif name == "Dan": # if what the user wrote equals "Bob" print("Hi Dan!") else: # if any of the previous conditionals are false print("Who are you?")

Lemme know if I was unclear or if you need more help! :)

Profile icon
MocaCDeveloper

You can do:

if input('Put whatever you want here: ') == "Whatever value you're looking for": print('Do some code here')

The above also works and is the shorter way of doing it!

Profile icon
AdityaMalik7

what to do if we want to write that if the user say this then the computer need to type this in python on replit