3 回答
TA贡献1802条经验 获得超10个赞
您只需要返回所选选项并在主代码中捕获返回值:
##### login, register or see the leaderboard.
def option():
option1 = int(input("""Would you like to:
1. Login
2. Register
3. See the Leaderboard"""))
return option1
##### MAIN CODE
choice = True
while choice == True:
option1 = option()
if option1 == 1:
login()
choice = False
elif option1 == 2:
register()
choice = False
elif option1 == 3:
leaderboardopen()
choice = False
else:
print("Incorrect value given. Please try again.")
TA贡献1943条经验 获得超7个赞
正如 Robert 和 Paritosh 上面提到的option1,在option函数之外不存在。我可以建议以下解决方案:
def option():
return int(input("Would you like to:"))
##### MAIN CODE
while choice == True:
option1 = option()
if option1 == 1:
login()
# continue your logic
TA贡献1818条经验 获得超7个赞
你还没有定义choice. 此外,您是option1在函数内部声明的,因此当您离开该函数时,它没有被定义。试试这个,你需要从函数中返回一些东西。
##### login, register or see the leaderboard.
choice = True
def option():
option = int(input("""Would you like to:
1. Login
2. Register
3. See the Leaderboard"""))
return option
##### MAIN CODE
while choice == True:
option = option()
if option == 1:
login()
choice = False
elif option == 2:
register()
choice = False
elif option == 3:
leaderboardopen()
choice = False
else:
print("Incorrect value given. Please try again.")
添加回答
举报