Python 提示计算器问题
试图为基于服务的tip计算器编写python代码时,我的while循环遇到了一个问题Python 提示计算器问题,python,while-loop,Python,While Loop,试图为基于服务的tip计算器编写python代码时,我的while循环遇到了一个问题 service = "" while service != "excellent" or service != "good" or service != "bad": service = input("(Please choose excellent, good, or bad): ") 此部分导致无限循环,但我不确定为什么或如何修复它…您使用的是或您想要使用的和 如果这三个条件中的任何一个为真,循环
service = ""
while service != "excellent" or service != "good" or service != "bad":
service = input("(Please choose excellent, good, or bad): ")
此部分导致无限循环,但我不确定为什么或如何修复它…您使用的是
或您想要使用的和
如果这三个条件中的任何一个为真,循环将继续
如果您的输入是“优秀”
,则服务!=“好”
和服务!=“bad”
都将为真,因此循环将继续。其他值也是如此
你想要的是:
service = ""
while service != "excellent" and service != "good" and service != "bad":
service = input("(Please choose excellent, good, or bad): ")
service = ""
while service != "excellent" and service != "good" and service != "bad":
service = input("(Please choose excellent, good, or bad): ")
更妙的是(正如各种评论家所指出的那样,更具蟒蛇风格的是):
这样读起来更容易,并且在任何情况下(上、下、混合)都可以接受输入。您使用的是或您想要使用的和
如果这三个条件中的任何一个为真,循环将继续
如果您的输入是“优秀”
,则服务!=“好”
和服务!=“bad”
都将为真,因此循环将继续。其他值也是如此
你想要的是:
service = ""
while service != "excellent" and service != "good" and service != "bad":
service = input("(Please choose excellent, good, or bad): ")
service = ""
while service != "excellent" and service != "good" and service != "bad":
service = input("(Please choose excellent, good, or bad): ")
更妙的是(正如各种评论家所指出的那样,更具蟒蛇风格的是):
这样读起来更容易,并且在任何情况下(上、下、混合)都可以接受输入。在这种情况下,您希望使用“and”而不是“or”。不过,另一个更干净的选择是:
while service not in ['excellent', 'good', 'bad]:
service = input('(Please choose excellent, good, or bad): ')
这将测试服务
的值是否在可接受答案列表中,并便于以后编辑。在这种情况下,您希望使用“and”而不是“or”。不过,另一个更干净的选择是:
while service not in ['excellent', 'good', 'bad]:
service = input('(Please choose excellent, good, or bad): ')
这将测试服务
的值是否在可接受答案列表中,并便于以后编辑。现在,您说的是“如果这些语句中的任何三个为真,请继续while循环”
你想要的是:
service = ""
while service != "excellent" and service != "good" and service != "bad":
service = input("(Please choose excellent, good, or bad): ")
service = ""
while service != "excellent" and service != "good" and service != "bad":
service = input("(Please choose excellent, good, or bad): ")
现在,您要说的是“如果这些语句中的任何三个为真,请继续while循环”
你想要的是:
service = ""
while service != "excellent" and service != "good" and service != "bad":
service = input("(Please choose excellent, good, or bad): ")
service = ""
while service != "excellent" and service != "good" and service != "bad":
service = input("(Please choose excellent, good, or bad): ")
你只是把逻辑搞混了-你用的是或
而不是和
无论你有什么意见,都会是(不好)或(不好)或(不坏)
例如,即使您输入“good”,good也是(不是极好的),因此总体条件返回True,并且您将继续循环
编写此条件的更清晰方法是:
while service not in ["excellent", "good", "bad"]:
service = input("(Please choose excellent, good, or bad): ")
你只是把逻辑搞混了-你用的是或
而不是和
无论你有什么意见,都会是(不好)或(不好)或(不坏)
例如,即使您输入“good”,good也是(不是极好的),因此总体条件返回True,并且您将继续循环
编写此条件的更清晰方法是:
while service not in ["excellent", "good", "bad"]:
service = input("(Please choose excellent, good, or bad): ")
要循环直到满足多个条件中的任何一个(因为一次满足所有条件既不可能也没有意义),您需要在不满足所有条件时循环(逻辑否定)。我能想到的表达这一点的最具可读性和python风格的方式是:
service = ''
while service not in ('excellent', 'good', 'bad'):
service = input("(Please choose excellent, good, or bad): ")
要循环直到满足多个条件中的任何一个(因为一次满足所有条件既不可能也没有意义),您需要在不满足所有条件时循环(逻辑否定)。我能想到的表达这一点的最具可读性和python风格的方式是:
service = ''
while service not in ('excellent', 'good', 'bad'):
service = input("(Please choose excellent, good, or bad): ")
我很确定你想用和
而不是或
别难过;很多初学者(甚至是专业人士!)都会犯这个错误!现在可以了!谢谢大家!:我很确定你想用和
而不是或
别难过;很多初学者(甚至是专业人士!)都会犯这个错误!现在可以了!谢谢大家!:您可能还应该调用service.lower()
更具Python风格的方式:如果服务不在[“优秀”、“好”、“坏”]:
您可能还应该调用service.lower()
更具Python风格的方式:如果服务不在[“优秀”、“好”、“坏”]: