对不起在宏观方案中对python很新,基本上我希望能够有一个输入屏幕,用户可以选择一个选项,当选择已经运行命令将运行,然后给用户选项返回选择另一个选项,或完全退出程序。
目前我有
print ("1.")
print ("2.")
print ("3.")
errorch=0
while not errorch :
try :
choice = int ( input('Please Choose from Options below :') )
errorch = 1
except ValueError as e :
print ("'%s' is not a valid integer." % e.args[0].split(": ")[1])
if choice == 1:
print ("CODE:")
elif choice == 2:
print ("OTHER:")
elif choice == 3:
print ("OTHER:")
else:
print ("Invalid Choice. Please Try Again:")
k=input('Press close to exit')
在每个选项中我都有运行的代码,但为了节省空间,我省略了这个
答案 0 :(得分:0)
使用while
循环。
while True: # this loop runs forever
print("1.")
...
print("4.") # this is for exit
# get input
if choice == 1:
...
# if choice is 4, then break out of this loop
elif choice == 4:
break # breaks out of the loop
else:
...
答案 1 :(得分:0)
您可以将整个内容包装在另一个while
循环中:
while True:
...
if k.lower() == "close":
break
您可以使用相同的表单使现有的循环更整洁,删除errorch
标记:
while True:
try:
choice = int(input('Please Choose from Options below :'))
except ValueError as e :
print ("'%s' is not a valid integer." % e.args[0].split(": ")[1])
else:
break