错误和异常循环

时间:2012-01-17 17:37:58

标签: python

您好我想循环我的程序,以便一旦它遇到异常,它就会从头开始重启!

>>> while True:
...     try:
...         x = int(raw_input("Please enter a number: "))
...         break
...     except ValueError:
...         print "Oops!  That was no valid number.  Try again..."

我该怎么做

1 个答案:

答案 0 :(得分:0)

您想删除break声明中的try。它告诉python退出while循环。

    try:
        x = int(raw_input("Please enter a number: "))
    except ValueError:
        print "Oops! That was no valid number. Try again..."