【问题标题】:Python I can't exit the while loop even though the condition is satisfiedPython即使满足条件,我也无法退出while循环
【发布时间】: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


【解决方案1】:

你的情况应该是

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

如果满足所有这 3 个条件,您希望它得到满足。使用or,如果其中任何一个条件满足,则整个条件都满足。

你可以用更短的方式写:

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

【讨论】:

    【解决方案2】:

    你的逻辑搞砸了。如果 perc=10 那么它一定不是 perc=12 但如果满足其中任何一个,你就会运行....尝试将您的 or 更改为 and 或者更好的尝试:

    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?"))
    

    【讨论】:

      【解决方案3】:

      您的条件 while 搞砸了,而且您使用 else,这就是您收到错误的原因。尝试而不是使用循环。

      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)
      

      【讨论】:

      • 如果用户第一次没有输入10、12、15,则不给总数。
      【解决方案4】:

      使用:

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

      更好(见上面的答案),

      但是要了解你的问题试试这个:

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

      结果是:

      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
      

      逻辑和,每个数字中的两个条件为真

      真 + 真 + 假 = 真

      真 + 假 + 真 = 真

      假 + 真 + 真 = 真

      【讨论】:

        猜你喜欢
        • 2021-08-07
        • 2021-11-18
        • 2020-04-15
        • 2023-01-12
        • 2016-03-14
        • 1970-01-01
        • 1970-01-01
        • 2013-04-08
        • 1970-01-01
        相关资源
        最近更新 更多