【问题标题】:python "break" error: break outside looppython“中断”错误:中断外部循环
【发布时间】:2018-01-24 17:07:01
【问题描述】:

当我开发我的第一个代码时,我遇到了break 命令的问题,如果出现错误,我尝试使用该命令重新启动程序。

看一下代码也许你会更好理解。

  Name = str(input("Please enter Your Name:"))
  Age = input("Please enter your age: ")
       if Age != int():
            print ("Error! Check the age")
            break
     elif Age == int():
          continue
  Height = input("Please enter your height: ")
    if Height != int():
         print ("Error! Check the Height")
             break
   elif Height == int():
        continue

 if Age == int() and Age >= 18 and Height == int() and Height >= 148:
  print("You're able to drive a car " + (Name) )

 elif Age == int() and Age < 18 and Height == int() and Height > 148:
    print("You're not able to drive a car " + (Name) )

 elif Age and Height != int() :
     print ("Error! , Age or Height are not numbers")

错误:

“C:\Users\Ghanim\Desktop\Coding\Documents\Projects\Python\Project1\Project1.py”,第 6 行 休息 ^ 语法错误:“中断”

外循环

【问题讨论】:

标签: python python-3.x


【解决方案1】:

break 语句用于退出循环,而不是程序。使用sys.exit()退出程序,还需要导入sys

编辑:

在回答您的评论时,我可能会这样做:

while True:

    inputted_name = input("Please enter your name:")

    try:
        name = str(inputted_name)
    except ValueError:
        print("Please enter a valid name")
    else:
        break


while True:

    inputted_age = input("Please enter your age:")

    try:
        age = int(inputted_age)
    except ValueError:
        print("Please enter a valid age")
    else:
        break


while True:

    inputted_height = input("Please enter your height:")

    try:
        height = float(inputted_height)
    except ValueError:
        print("Please enter a valid height")
    else:
        break


if age >= 18 and height >= 148:
    print("You're able to drive a car {}".format(inputted_name))

if age < 18 and height > 148:
    print("You're not able to drive a car {}".format(inputted_name))

所以有一些变化:

用户输入的每个阶段都在自己的循环中。我使用了 try/except/else 语句,它们尝试将输入转换为正确的类型,除了 ValueErrors (如果不能转换则抛出,例如,如果用户对 input age 提供文本答案,则会发生这种情况. 如果它成功转换为正确的类型,则循环被打破,脚本移至下一个。每个循环都有单独的循环意味着如果用户为其中一个输入不正确的值,他们不必重做整件事。

我还使用format()name 插入到最终字符串中,以避免进行字符串连接。

另外,请注意,我假设您为此使用 Python 3。但是,如果您使用的是 Python 2,则应将 input() 替换为 raw_input()。在 Python 2 中,input() 将尝试将用户输入评估为表达式,而raw_input() 将返回一个字符串。

【讨论】:

  • 但是出现错误时如何让程序从头开始?
  • 在这种情况下,您需要一个 while 循环来进行输入调用,我会更新我的答案给您一个示例
  • 非常感谢,非常感谢 :)
  • 乐于助人! :)
  • 我在第 37 行和第 40 行发现了一个问题 TypeError: unorderable types: str() &gt;= int() 所以我通过将 int() 添加到 inputted_age and inputted_height 来解决这个问题,这样它就变成了 int(inputted_age) and int(inputted_height) 只要你可以在为其他观众发表评论。
【解决方案2】:

break 语句跳出循环(for 循环或 while 循环)。在此上下文之外,它没有任何意义。

【讨论】:

    【解决方案3】:

    break 不能重启你的程序,break 只能用在 for 或 while 等循环中。

    在你的情况下,只需使用 exit(-1)

    【讨论】:

      【解决方案4】:

      您的程序中没有循环。 break 不能在循环外使用。您可以使用 sys.exit() 代替 breakpass 代替 continue。

      【讨论】:

        猜你喜欢
        • 2011-01-28
        • 1970-01-01
        • 2013-06-07
        • 1970-01-01
        • 2019-09-26
        • 1970-01-01
        • 1970-01-01
        • 2012-09-20
        • 1970-01-01
        相关资源
        最近更新 更多