Python 无限循环';试试看,如果';用某种方法
执行Python 无限循环';试试看,如果';用某种方法,python,if-statement,try-catch,Python,If Statement,Try Catch,执行菜单()并转到1、2或3有助于完成其工作 但是在经过getproduct(character)并返回menu()之后,如果选择数字3,则会产生一个坏循环 我想知道为什么,以及如何解决这个问题 def menu(): menu = '1. ice\n2. cream\n3. quit' print(menu) try: order = int(input('choose one: ')) if order
菜单()
并转到1、2或3有助于完成其工作
getproduct(character)
并返回menu()
之后,如果选择数字3,则会产生一个坏循环
def menu():
menu = '1. ice\n2. cream\n3. quit'
print(menu)
try:
order = int(input('choose one: '))
if order == 1:
c = 'ice'
getproduct(c)
elif order == 2:
c = 'cream'
getproduct(c)
elif order == 3:
exit()
else: menu()
except ValueError: menu()
def getproduct(character):
toping = int(input('1. ice or 2. cream?'))
try:
if character == 'ice' and toping == 1:
print(character + 'ice')
menu()
elif character == 'ice' and toping == 2:
print(character + 'cream')
menu()
elif character == 'cream' and toping == 1:
print(character + 'ice')
menu()
elif character == 'cream' and toping == 2:
print(character + 'cream')
menu()
else: getproduct(character)
except: getproduct(character)
menu()
exit()
这是一个例外情况,这意味着上面引发SystemExit
的代码可以关闭资源并执行其他关闭活动,以避免破坏任何外部资源。但是,这也意味着一个空白的语句,除了:
之外,可以捕获SystemExit
并忽略它。这就是这里发生的事情
def getproduct(character):
toping = int(input('1. ice or 2. cream?'))
try:
...
# call to menu(), which calls exit()
except: # this activates on ANY exception, including SystemExit
getproduct(character)
一般来说,除了
块,您几乎不应该使用原始的,因为在这种情况下,它会捕获您不希望捕获的内容。相反,分析try
中的代码,找出它将抛出的异常类型,并捕获这些异常。在您的情况下,可能是ValueError
或TypeError
:
try:
...
except (ValueError, TypeError):
getproduct(character)
或者,如果您一心想捕获所有内容,您可以编写一个特殊的异常,以确定错误是否为SystemExit
(不过,空白除外:
或除外例外:
被视为不好的做法):
您应该注意,根据:
退出(code=None)
退出(code=None)
对象,该对象在打印时打印消息,如“使用quit()或Ctrl-D(即EOF)退出”,并在调用时使用指定的退出代码引发SystemExit
您可能应该改用sys.exit()
,尽管它的功能基本相同。代码中有几个错误。首先,您使用的是exit,它不应该在文件中使用,取而代之的是,我在我的示例中使用了具有相同目标的模块sys(sys.exit(0))
另一方面,您正在以不精确的方式使用输入检查,循环应该是不同的。在菜单中,我个人建议使用while循环
您问题的解决方案和一些改进(可能更好):
你说的坏循环是什么意思?我想他的意思是,它落在一个3
选项上,因为他试图调用exit()
你的函数是相互递归的。我肯定你不想那样。也许你的意思是getproduct()
应该返回菜单()
。你还应该看看坏循环意味着无限循环。顺便说一句,你不需要sys.exit(0)
。我更喜欢return
@quamrana您是对的,但是,我通常在明确用于退出过程的环境中使用return
,在想要结束应用程序的环境中使用sys.exit(0)
。在这种情况下,两种情况是相同的,所以我同意,谢谢。这就是我想做的事情。
try:
...
except SystemExit:
pass # ignore it
except:
getproduct(character)
import sys
def menu():
menu = '1. ice\n2. cream\n3. quit'
while True:
print(menu)
try:
order = int(input('choose one: '))
except:
print("Use a correct answer")
else:
if order == 1:
c = 'ice'
getproduct(c)
elif order == 2:
c = 'cream'
getproduct(c)
elif order == 3:
sys.exit(0)
else:
print("Use a correct answer")
def getproduct(character):
topings = '1. ice or 2. cream?: '
while True:
print(topings)
try:
second_order = int(input())
except:
print("Use a correct answer")
else:
if character == 'ice' and second_order == 1:
print(character + 'ice')
break
elif character == 'ice' and second_order == 2:
print(character + 'cream')
break
elif character == 'cream' and second_order == 1:
print(character + 'ice')
break
elif character == 'cream' and second_order == 2:
print(character + 'cream')
break
else:
print("Use a correct answer.")
menu()