0

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')
1
  • Type in 'True or False' at the REPL and see what you get. Commented Mar 12, 2018 at 2:03

6 Answers 6

1

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)

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

Comments

0

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

I think there's an opportunity here for some education. the hungry variable goes from None, to a boolean, to a string, to a boolean, etc. It's workable in this little scenario, but I don't think the OP will learn much out of this approach. I've added an answer below to explain.
Good point. My focus was to get what they had written working, but it's definitely bad style to keep changing the type of hungry.
But impressive on how you were able to keep to the original style ;-p
0

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

Or you can also use if x in (a, b, c)
0

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

0

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

You're right, I haven't covered membership statements, and your code is familiar with what I have covered so far. However I found Pdubbs the most useful as it looks cleaner with less code and I didn't know about the in statements beforehand. Thanks for the help
@NathanByrne Yeah, I understand. No problem. It was my pleasure.
Note, you can write hungry != True or False, but python short circuit evaluation on or will return True, so the comparison is always hungry != True
0

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')

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.