2017-06-21 568 views
2

刚从这里开始soz。我一直试图通过输入一个数字来创建一个带有几个选项的菜单(def logged() :),并且它会跳转到该功能的目的。但是,我似乎无法使用while语句放入if语句来调用指定函数,而是在日志函数应该永久保持while循环运行时跳转到menu()函数。Python:无法在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") 

回答

2

好的,所以你犯了一些错误(很明显),没什么大不了的,每个人都得开始学习。

最大的问题是你进入菜单循环(你有第二个while循环),但从不做任何事情来退出它。我还评论了其他一些变化。我不是100%确定你在某些地方做什么...但是...

我认为this is what you were going for though,我评论了这些变化。有一些奇怪的东西,我只是留下,因为我认为这是意图。

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") 
+1

作为一个原则问题,不要发布不包含代码的答案。当链接由于某种原因消失时,你的回答将变得毫无价值。你仍然可以链接到repl.it,但也可以在这里复制你的代码。 – Tomalak

+0

@Tomalak我gottcha好朋友。 –

1

的问题是,你的代码犯规跟着你想要的流动路径,尝试上面的代码,看看是否是你想要的,我会考虑的一点点,试图解释我做了什么(现在我只是创建了一个函数whileloop()并将其添加到正确的位置)。

def whileloop(): 
    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") 
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") 
    whileloop() 

def test2(): 
    print("Test2") 
    whileloop() 

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 """) 

我觉得发生了什么事。我列出你的代码正在经历的流程,你可以用简单的方法理解它。

  1. 进入循环while validintro;
  2. 进入while True环(chosen_option = menu()
  3. 进入menu(),并呼吁test1()
  4. 进入test1(),并呼吁logged()
  5. 进入logged()和现在的事情。当您在While True循环中调用test1()函数时,您的执行流程将返回到。
  6. 您输入if chosen_option in ['b', 'B']
  7. 因为它不在b,B你会激活你的else声明并打印你的错误信息。之后,循环重新开始。