Skip to content
Sign upLog in
← Back to Community

Can some one help me with this?

Profile icon
PizzaMan21

I trying to make it to where it asks you if the sky is is blue and if you input “True” or “true” or “T” or “t”, than it says your correct or if you input “False” or “false” or “F” or “f”, then it says your incorrect. If you input anything else besides those eight options, then it tells you invalid answer.

Preferably with “response ==” or “response !=” in the if and elif.

Can someone please give me some guidance?

Answered by 19wintersp [earned 5 cycles]
View Answer
Voters
Profile icon
PizzaMan21
Comments
hotnewtop
Profile icon
19wintersp

You can check if it is in a list of options:

if response in ["true", "True", "T", "t"]: print("That is correct!") elif response in ["false", "False", "F", "f"]: print("That is incorrect!") else: print("You entered an invalid answer.")

That code uses the in keyword to check if the response is in the list.

Profile icon
PizzaMan21

@19wintersp
is there a way for it to work with “response ==” or “response !=”?

Profile icon
19wintersp

@KeenanBusch
What do you mean? It has the same effect. If you wanted to use ==, you'd have to do something like:

if response == "true" or response == "True" or response == "T" or response == "t":

which is unwieldy.

Profile icon
PizzaMan21

Oh

@19wintersp
I thought it would have been something like:

if response == “true” or “True” or “T” or “t”:

Profile icon
19wintersp

@KeenanBusch
Unfortunately, you can't do that: == is the comparison operator, and you put one value on each side. Your example would be parsed as:

if ( (response == "true") or ("True") or ("T") or ("t") ):

and since non-empty strings* are truthy in Python, it is equivalent to:

if ( (response == "true") or (true) or (true) or (true) ):
Profile icon
PizzaMan21

Ok

@19wintersp
thank you

Profile icon
RYANTADIPARTHI

Solution

try this code. Use input() for it.

sky = input('is the sky blue ? ') if 'T' in sky or 't' in sky: print('correct!') elif 'f' in sky or 'F' in sky: print('incorrect') else: print('invalid')

like that.
That should work

Profile icon
Dunce

if you don't want to input the different capitalizations into your if statements you can also put .lower() at the end of your input command