Python:在while循环中无法调用函数

4

很抱歉,我刚开始工作。我一直在尝试制作一个有几个选项 (def logged()) 的菜单,并通过输入数字来跳转到具有特定目的的函数。然而,在 while 循环中放置 if 语句时,我似乎无法调用指定的函数,反而会跳回 menu() 函数,而 logged 函数应该永远通过 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个回答

2

好的,显然你犯了一些错误,不过没关系,每个人都得从某个地方开始学习。

最大的问题是你进入了菜单循环(你有的第二个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")  

2
原则上,不要发布不包含代码的答案。当链接因某种原因消失时,您的答案将变得毫无价值。您仍然可以链接到repl.it,但也要在此处复制您的代码。 - Tomalak

2

问题在于您的代码没有遵循您想要的流程路径,请尝试上面的代码,看看是否符合您的要求。我会思考一下并尝试解释我所做的事情(目前我只创建了一个名为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语句并打印错误消息。之后,循环重新开始。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接