简体   繁体   English

Python while循环布尔仅显示为True

[英]Python while loop boolean only showing as True

I'm having trouble. 我有麻烦了 I want the code to loop through and ask 'Hey, are you hungry?' 我希望代码循环通过,然后问: 'Hey, are you hungry?' but only if the hungry variable isn't True or False . 但仅当饥饿变量不是TrueFalse However, whatever answer I type in, it acts as if it's True . 但是,无论我输入什么答案,它都好像是True

In my head, if I type a variation of yes, then hungry should be True and the code should stop. 在我的脑海中,如果输入的是yes,则饥饿应为True ,并且代码应停止。 If I type a variation of no then the code should be False and stop. 如果输入的是no,则代码应为False并停止。 If I type neither of these then hungry will not be True or False and should loop through, asking the question again. 如果我没有输入任何一个,那么饥饿将不会是TrueFalse ,应该遍历,再次询问问题。

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

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 ). 您在这里遇到了一些问题,尤其是出于几个不同的目的而使用了相同的变量名,这不是一个好的样式,也没有真正使用python中的控制流原语-查找它( 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) 我们正在开始无限循环(而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 如果用户在“是”或“否”上输入变体,则会打印响应并退出while循环

  • if they don't provide a yes/no answer, it goes back to the beginning of the loop again (ie no break) 如果他们不提供是/否的答案,它将再次返回到循环的开头(即不间断)

You can't say if x == a or b or c in Python and have it do what you want. 您无法说出if x == a or b or c在Python中是否让其满足您的要求。 You need to use if x in [a, b, c] instead. 您需要使用if x in [a, b, c]代替。

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. hungry == 'yes' or 'ye' or 'y' or 'yeah'不会检查是否有任何hungry == 'yes'hungry == 'ye'hungry == 'y'hungry == 'yeah'是真的。 It checks (hungry == yes) or 'ye' or 'y' or 'yeah' (and nothing but hungry == yes has a truth value) 它检查(hungry == yes) or 'ye' or 'y' or 'yeah' (除了hungry == yes ,没有其他值hungry == yes真值)

Similarly hungry != True or False is checking (hungry != True) or False so the False part of the conditional doesn't do anything. 同样, hungry != True or False正在检查(hungry != True) or False因此条件的False部分不会执行任何操作。

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

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

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 . 因此, while hungry != True or False应该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. 对所有其他ifwhile语句执行相同的操作。 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. 但是,由于您似乎是所编写代码的初学者,因此我认为我的解释将使您更容易理解。

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 != True or False ,这是代码的有效版本:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM