尝试和除了不循环 - Python

时间:2016-02-05 08:24:06

标签: python python-3.x

我试图循环尝试除了我的菜单选项,部分代码如下,我只是想知道如何无限循环这个以避免程序崩溃...我尝试了一段时间真正的循环但我失败!

错误:http://imgur.com/o3F5phb

while True:
    try:
       choice = int(input("What would you like to do?\n1)Code a word\n2)Decode a word\n3)Print the coded words list\n4)Quit the program\n5)Clear Coded Words List\n>>>"))#Asking for choice from user
    except ValueError:
        print("Invalid entry! Try again")
        choice =int(input("What would you like to do?\n1)Code a word\n2)Decode a word\n3)Print the coded words list\n4)Quit the program\n5)Clear Coded Words List\n>>>"))#Asking for choice from user
        continue
    else:
        break

2 个答案:

答案 0 :(得分:3)

尝试一下

Grid objTextbox = (Grid)sender;

答案 1 :(得分:0)

如果你想让这个代码片段循环,你需要引入一个循环。 E.g:

input_valid = False
while not input_valid:
    try:
        choice=int(input("What would you like to do?\n1)Code a word\n2)Decode a word\n3)Print the coded words list\n4)Quit the program\n5)Clear Coded Words List\n>>>"))#Asking for choice from user
        input_valid = True
    except ValueError:
        print("Invalid Entry!\nPlease Try Again\n")