Skip to main content
 首页 » 编程设计

Python:循环不退出 "break"

2025年05月04日13insus

我有一个 while 循环,它正在测试以确保用户输入 1、2 或 3。一旦用户输入 1、2 或 3,循环就会无限循环,我无法摆脱它.

while True: 
    try: 
         filterselection = raw_input("Please select a filter (1, 2, or 3): ") 
         if filterselection == "1" or filterselection == "2" or filterselection == "3": 
              filterselection = int(filterselection) 
              break 
 
      else: 
          print "Not a valid number try again!" 
      except TypeError: 
          print "Lol, that's not a number try again!" 

请您参考如下方法:

不要混用制表符和空格!这是我将原始代码粘贴到显示空白字符的编辑器时看到的内容:

箭头是制表符,点是空格,不要混淆这些非常重要,因为如果你执行代码,你看到的可能不是 Python 解释器看到的。