刚从这里开始。我一直在尝试def logged()
通过输入数字来制作带有多个选项(:)的菜单,该菜单会有目的地跳转到该功能。但是,我似乎无法在while循环中放置if语句来调用指定的函数,相反,menu()
当记录的函数应该永远在while循环中运行时,它将跳回到该函数。
当我在的菜单中输入相应的数字时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")
好的,所以您犯了一些错误(显然),没什么大不了的,每个人都必须在某个地方开始学习。
最大的问题是您进入菜单循环(第二个while循环),但是从不进行任何退出。我还评论了其他几处更改。我不确定100%在某些地方要做什么...但是...
我认为这是您想要的,我评论了这些更改。我有些遗漏的东西,因为我认为那是意图。
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 = False # I like it this way
while not 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 = True
print("Welcome to the test program {}.".format(name))
#The main routine
validintro = False # need a way out
while not validintro:
chosen_option = menu() #a custom variable is created that puts the menu function into the while loop
validintro = True # start thinking we're okay
if chosen_option in ["a", "A"]:
test1() # you're calling this, which calls the logged thing, but you do nothing with it
# I just left it because I figured that's what you wanted
elif chosen_option in ["b", "B"]: # You want an elif here
test2()
else:
print("""That was not a valid option, please try again:\n """)
validintro = False # proven otherwise
validintro = False
while not validintro:
validintro = True
option = logged()
print(option)
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: ")
validintro = False
print("Goodbye")
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句