I'm having trouble. I want the code to loop through and ask 'Hey, are you hungry?' but only if the hungry variable isn't True or False. However, whatever answer I type in, it acts as if it's True.
In my head, if I type a variation of yes, then hungry should be True and the code should stop. If I type a variation of no then the code should be False and stop. If I type neither of these then hungry will not be True or False and should loop through, asking the question again.
hungry = None
while hungry != True or False:
hungry = input('Hey, are you hungry?')
if hungry == 'yes' or 'ye' or 'y' or 'yeah':
print ('oh you hungry huh')
hungry = True
elif hungry == 'no' or 'n' or 'nah' or 'nope':
print ('no food for you then')
hungry = False
else:
print ('its a simple yes or no question pls')
-
Type in 'True or False' at the REPL and see what you get.Marcin– Marcin2018年03月12日 02:03:01 +00:00Commented Mar 12, 2018 at 2:03
6 Answers 6
You have a few issues here, not least is using the same variable name for a few different purposes which is not good style, and also not really using the control flow primitives in python - look it up (https://wiki.python.org/moin/WhileLoop).
A concise a working solution would look as follows:
while True:
hungry = input('hungry?')
if hungry in ('y','yes'):
print('Eat up')
break
elif hungry in ('n','no'):
print('Ok not hungry')
break
else:
print('You need to tell me')
What we've got above is:
We're starting an infinite loop (while True)
We're assigning the user input to the hungry variable - and that's all it holds
If the user enters a variant on 'yes' or 'no' it prints a response and breaks out of the while loop
if they don't provide a yes/no answer, it goes back to the beginning of the loop again (ie no break)
Comments
The condition hungry == 'yes' or 'ye' or 'y' or 'yeah' doesn't check if any of hungry == 'yes' or hungry == 'ye' or hungry == 'y' or hungry == 'yeah' is true. It checks (hungry == yes) or 'ye' or 'y' or 'yeah' (and nothing but hungry == yes has a truth value)
Similarly hungry != True or False is checking (hungry != True) or False so the False part of the conditional doesn't do anything.
You should write it as:
hungry = None
while hungry not in (True, False):
hungry = input('Hey, are you hungry?')
if hungry in ('yes', 'ye', 'y', 'yeah'):
print ('oh you hungry huh')
hungry = True
elif hungry in ('no', 'n', 'nah', 'nope'):
print ('no food for you then')
hungry = False
else:
print ('its a simple yes or no question pls')
3 Comments
You can't say if x == a or b or c in Python and have it do what you want. You need to use if x in [a, b, c] instead.
1 Comment
if x in (a, b, c)Try it this way:
hungry = None
while hungry not in (True, False):
hungry = input('Hey, are you hungry?')
if hungry in ('yes', 'ye', 'y', 'yeah'):
print('oh you hungry huh')
hungry = True
elif hungry in ('no', 'n', 'nah', 'nope'):
print('no food for you then')
hungry = False
else:
print('its a simple yes or no question pls')
Comments
This is because you are writing code like it is english. When you are comparing a variable to two booleans you to first compare the variable to the first boolean and then compare the variable to the second boolean. So while hungry != True or False should be while hungry != True or hungry != False. If you use the later, you will get the wrong answer. Do the same for all your other if and while statements. So your code should be:
hungry = None
while hungry != True or hungry == False:
hungry = input('Hey, are you hungry?')
if hungry == 'yes' or hungry == 'ye' or hungry == 'y' or hungry == 'yeah':
print ('oh you hungry huh')
hungry = True
elif hungry == 'no' or hungry =='n' or hungry =='nah' or hungry =='nope':
print ('no food for you then')
hungry = False
else:
print ('its a simple yes or no question pls')
Edit: Others are suggesting membership statements which work too. However, since it looks like you're a beginner from the code you have written, I think my explanation will be easier for you to understand.
3 Comments
Your code didn't work because you're using statements in the wrong way, such as hungry != True or False, here's a working version of your code:
hungry = None
while hungry not in (True, False): # Check if hungry is not True or False
hungry = input('Hey, are you hungry?')
if hungry in ('yes', 'ye', 'y', 'yeah'):
print ('oh you hungry huh')
hungry = True
elif hungry in ('no', 'n', 'nah', 'nope'):
print ('no food for you then')
hungry = False
else:
print ('its a simple yes or no question pls')