简体   繁体   English

提示计算器问题

[英]Tip calculator issue

trying to write a python code for a tip calculator based on service and I'm running into an issue with my while loop. 试图为基于服务的小费计算器编写python代码,而我的while循环遇到问题。

service = ""
while service != "excellent" or service != "good" or service != "bad":
    service = input("(Please choose excellent, good, or bad): ")

This part is causing an infinite loop, but I'm not sure why or how to fix it... 这部分导致无限循环,但我不确定为什么或如何解决...

You are using or where you want to use and . 您正在使用or要在何处使用and

The loop is going to continue if any one of the three conditions is true. 如果三个条件之一为真,则循环将继续。

If your input is "excellent" then service != "good" and service != "bad" are both going to be true so the loop will continue. 如果您的输入是"excellent"那么service != "good"service != "bad"都是正确的,因此循环将继续。 The same is true for the other values. 其他值也是如此。

What you want is: 您想要的是:

service = ""
while service != "excellent" and service != "good" and service != "bad":
    service = input("(Please choose excellent, good, or bad): ")

Even better (and more Pythonic as various commenters have pointed out) is: 更好的(以及各种评论者指出的更多Pythonic的)是:

service = ""
while service.lower() not in ["excellent", "good", "bad"]:
    service = input("(Please choose excellent, good, or bad): ")

This reads easier and also accepts inputs in any case (upper, lower, mixed). 这更容易阅读,并且在任何情况下(上,下,混合)都接受输入。

In this situation you want to use "and" rather than "or". 在这种情况下,您要使用“和”而不是“或”。 Another option, though, that is cleaner would be to do this: 但是,另一个更干净的选择是执行以下操作:

while service not in ['excellent', 'good', 'bad]:
    service = input('(Please choose excellent, good, or bad): ')

This will test whether the value of service is in the list of acceptable answers and makes it easier to edit later. 这将测试service的价值是否在可接受的答案列表中,并使以后的编辑更加容易。

Right now, you're saying "if any three of these statements is true, continue the while loop". 现在,您要说的是“如果其中任何三个语句为真,则继续while循环”。

What you want is: 您想要的是:

service = ""
while service != "excellent" and service != "good" and service != "bad":
    service = input("(Please choose excellent, good, or bad): ")

You just have your logic a bit mixed up - you are using or instead of and . 您只是将逻辑混为一谈-您正在使用or代替and

Whatever input you have, it will be (not excellent) or (not good) or (not bad)! 无论您有什么输入,都是(不好)或(不好)或(不错)!

For example, even if you input "good", good is (not excellent) and thus the overall condition returns True, and you'll keep looping. 例如,即使您输入“ good”,good仍为(不是优秀),因此总体条件返回True,并且您将继续循环。

A clearer way to write this condition is: 编写此条件的更清晰方法是:

while service not in ["excellent", "good", "bad"]:
    service = input("(Please choose excellent, good, or bad): ")

To loop until any of a number of conditions is met (because it is neither possible nor meaningful to meet all of them at once), you need to loop while all of them are not met (logical negation). 循环,直到任意数量的条件得到满足(因为它是既不可能也没有意义,以满足所有的人都在一次),你需要循环,而所有的人都得不到满足(逻辑否定)。 The most readable and pythonic way to express this that I can think of is: 我能想到的最易读和Python式的表达方式是:

service = ''
while service not in ('excellent', 'good', 'bad'):
    service = input("(Please choose excellent, good, or bad): ")

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

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