这是我的代码的一小部分
def menu():
choices = ["Create a Scenario", "Load a Database", "Quit"]
m1 = eg.choicebox("What would you like to do?", "Greenfly Model Menu", choices)
if m1 == "Load a Scenario":
dbless = eg.enterbox("What is the name of your database?")
if dbless is None:
menu()
db_name = dbless + ".db"
check = os.path.isfile(db_name)
如果变量 dbless 最终为无,则代码最终会运行 menu()函数。但是,其余代码完成执行后,此函数的其余部分最终运行。无论如何都要使其功能的其余部分不能运行。
答案 0 :(得分:0)
使用return
语句而不是在没有循环的函数内部考虑break
。
答案 1 :(得分:0)
你可以包含一个while
循环,这样在输入不是None之前,它会要求输入:
while dbless is None:
menu()
else: #you can remove the else and unindent the next two lines, but I'm used to do it that way
db_name = dbless + ".db"
check = os.path.isfile(db_name)
我在这里看到的问题是它将无限期地询问输入。如果您遇到问题,可以添加类似attempt
的内容,但我不认为是这种情况。此外,我不确定该代码是否真的有效,因为再次调用该函数...无论如何,试一试。
答案 2 :(得分:0)
你不应该在循环外使用break语句。因此,您需要使用return语句。你可以用两种方式做到这一点。
if dbless is None:
menu()
return
或
if dbless is None:
return menu()
答案 3 :(得分:0)
您可以尝试这一点,它将符合您的逻辑:
def menu():
choices = ["Create a Scenario", "Load a Database", "Quit"]
m1 = eg.choicebox("What would you like to do?", "Greenfly Model Menu", choices)
if m1 == "Load a Scenario":
dbless = eg.enterbox("What is the name of your database?")
flag=0
while flag<1:
if dbless is None:
menu()
break
db_name = dbless + ".db"
check = os.path.isfile(db_name)
flag+=1