简体   繁体   English

Python 即使条件满足也无法退出while循环

[英]Python I can't exit the while loop even though the condition is satisfied

I tried to write a code that calculates the amount of bill everyone should pay after adding the tip but I wanted to limit the user to certain tip percentages and give them an error if they picked something else.我试图编写一个代码来计算每个人在添加小费后应该支付的账单金额,但我想将用户限制在特定的小费百分比,如果他们选择了其他东西,就会给他们一个错误。

So, I came up with this:所以,我想出了这个:

print("Welcome to the tip calculator.")

bill = float(input("What was the total bill?"))

people = int(input("How many people to split the bill?"))

perc = int(input("What percentage tip would you like to give? 10, 12, or 15?"))


total = float((bill + (bill * perc / 100)) / people)


while perc != 10 or perc != 12 or perc != 15:
    print("Error, please chose one of the given percentages.")
    perc = float(input("What percentage tip would you like to give? 10, 12, or 15?"))


else:
    print("Everyone should pay", total)

But, even though I enter 10, 12, or 15 I get the " Error, please chose one of the given percentages."但是,即使我输入 10、12 或 15,我也会收到“错误,请选择给定百分比之一”。 message.信息。 What should I do?我应该怎么办?

Your condition should be你的情况应该是

if perc != 10 and perc != 12 and perc != 15:   

You want it satisfied if all of these 3 conditions are satisfied.如果满足所有这三个条件,您希望它得到满足。 Using or , the whole condition would be satisfied if any of them was.使用or ,如果其中任何一个条件满足,则整个条件都满足。

You could write is in a shorter manner:你可以用更短的方式写:

if perc not in [10, 12, 15]:

Your logic is messed up.你的逻辑很混乱。 if perc=10 then it must NOT be perc=12 but you run if either of those are met.... try changing your or to and or better yet try:如果 perc=10 那么它一定不是 perc=12 但如果满足其中任何一个,你就会运行....尝试将您的or更改为and or 更好的尝试:

while perc not in [10, 12, 15]:
    print("Error, please chose one of the given percentages.")
    perc = float(input("What percentage tip would you like to give? 10, 12, or 15?"))

Your condition while is messed up and also you use else that's why you're getting error.你的情况虽然搞砸了,而且你使用 else 这就是为什么你会出错。 Try instead of using a loop.尝试而不是使用循环。

print("Welcome to the tip calculator.")
bill = float(input("What was the total bill?"))
people = int(input("How many people to split the bill?"))
perc = int(input("What percentage tip would you like to give? 10, 12, or 15?"))
total = float((bill + (bill * perc / 100)) / people)
while perc not in [10, 12, 15]:
    print("Error, please chose one of the given percentages.")
    perc = float(input("What percentage tip would you like to give? 10, 12, or 15?"))
else:
    print("Everyone should pay", total)

use:利用:

while perc not in [10, 12, 15]:

is better (see answers above),更好(见上面的答案),

but to understand You problem try this:要了解您的问题,请尝试以下操作:

while perc != 10 or perc != 12 or perc != 15:
    perc = float(input("What percentage tip would you like to give? 10, 12, or 15?"))
    print("10: ", perc != 10, "12: ", perc != 12, "15: ", perc != 15, "OR: ", perc != 10 or perc != 12 or perc != 15)`

Result is:结果是:

What percentage tip would you like to give? 10, 12, or 15?10
10.0
10:  False 12:  True 15:  True OR:  True
What percentage tip would you like to give? 10, 12, or 15?12
12.0
10:  True 12:  False 15:  True OR:  True
What percentage tip would you like to give? 10, 12, or 15?15
15.0
10:  True 12:  True 15:  False OR:  True

or is logical sum , in every numbers two of the condition is tru or逻辑和,每个数字中的两个条件为真

True + True + False = True真 + 真 + 假 = 真

True + False + True = True真 + 假 + 真 = 真

False + True + True = True假 + 真 + 真 = 真

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

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