【发布时间】:2023-03-11 01:50:01
【问题描述】:
我试图编写一个代码来计算每个人在添加小费后应该支付的账单金额,但我想将用户限制在特定的小费百分比,如果他们选择了其他东西,就会给他们一个错误。
所以,我想出了这个:
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)
但是,即使我输入 10、12 或 15,我也会收到 “错误,请选择给定百分比之一。” 消息。我该怎么办?
【问题讨论】:
-
使用
while perc not in (10,12,15):
标签: python python-3.x loops while-loop do-while