【发布时间】:2017-11-24 08:35:18
【问题描述】:
刚从这里开始。我一直在尝试通过输入一个数字来制作一个包含多个选项的菜单 (def logged():),它会跳转到该功能。但是,我似乎无法使用放入 while 循环中的 if 语句来调用指定的函数,而是当记录的函数应该永远在 while 循环中运行时,它会跳转回 menu() 函数。
当我在logged()的菜单中输入相应的数字时,它应该调用那个特定的函数,但它只是跳回到第一个菜单。我似乎无法让这两个菜单在不来回跳动的情况下永远循环。那么我究竟如何让两个while循环永远分开循环而不是相互循环呢?
def menu():
mode = input("""Choose options:\n
a) Test1 Calls logged() function
b) Test2
Enter the letter to select mode\n
> """)
return mode
def test1():
print("Test1")
logged()
def test2():
print("Test2")
def logged(): #Logged menu is supposed to run through a while loop and not break out when reached.
print("----------------------------------------------------------------------\n")
print("Welcome user. ")
modea = input("""Below are the options you can choose:\n
1) Function1
2) Function2
3) Function3
4) Exit
\n
Enter the corresponding number
> """).strip()
return modea
def funct1(): #EXAMPLE FUNCTIONS
print("Welcome to funct1")
def funct2():
print("Welcome to funct2")
def funct3():
print("Welcome to funct3")
#Main routine
validintro = True
while validintro:
name = input("Hello user, what is your name?: ")
if len(name) < 1:
print("Please enter a name: ")
elif len(name) > 30:
print("Please enter a name no more than 30 characters: ")
else:
validintro = False
print("Welcome to the test program {}.".format(name))
#The main routine
while True:
chosen_option = menu() #a custom variable is created that puts the menu function into the while loop
if chosen_option in ["a", "A"]:
test1()
if chosen_option in ["b", "B"]:
test2()
else:
print("""That was not a valid option, please try again:\n """)
while True:
option = logged()
if option == "1":
funct1()
elif option == "2":
funct2()
elif option == "3":
funct3()
elif option == "4":
break
else:
print("That was not a valid option, please try again: ")
print("Goodbye")
【问题讨论】:
标签: python-3.x function while-loop break