python - continue 语句在函数中是如何工作的?
问题描述
我正在建造科学计算器。执行操作后询问你是要返回主菜单还是退出
def Again():
x=input ("Go back to main menu(Y/N)")
if (x=='Y') or (x=='y'):
continue
else:
break
当用户按 y 时返回主菜单,否则退出
解决方案
你不能在函数中使用break
和continue
,阅读教程
相反,您可以在主循环中使用检查,并且您的函数必须返回True
或False
def Again():
x=input ("Go back to main menu(Y/N)")
if (x=='Y') or (x=='y'):
return True
else:
return False
while True: # your main loop
# some other code
# check on exit
if Again():
continue
else:
break