python-3.x - python : Unable to call function when in while loop

标签 python-3.x function while-loop break

刚从这里开始soz。我一直在尝试通过输入一个数字来制作一个带有多个选项的菜单 (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")  

最佳答案

问题是你的代码没有遵循你想要的流程,试试上面的代码,看看这是否是你想要的,我会考虑一下并尝试解释我所做的(现在我刚刚创建了一个函数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 """)

我想通了是怎么回事。我将列出您的代码正在经历的流程,您可能会以简单的方式理解它。
  • 进入循环 while validintro ;
  • 进入 while True循环 ( chosen_option = menu() )
  • 进入 menu()并调用 test1()
  • 进入 test1()并调用 logged()
  • 进入 logged()现在就是这样。当您调用 test1() 时,您的执行流程将返回到While True 循环中的函数。
  • 您输入 if chosen_option in ['b', 'B'] .
  • 由于它不在 b,B 内,您将激活您的 else语句并打印您的错误消息。之后,循环重新开始。
  • 关于python-3.x - python : Unable to call function when in while loop,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44676100/

    相关文章:

    python - 使用现有函数,如何定义具有不同默认可选参数的新函数?

    python - 可以使用 python 3 中的类型提示生成文档字符串吗?

    php - mysql函数的返回结果

    c++ - C++ 中 main 的正确声明是什么?

    java - 在 Java 中循环输出到 .txt 文件

    C++从文件函数读取无法在循环时退出

    python - 来自字符串中定义的函数的 `inspect.getsource`? `s="def f() : return 5 "`

    python - 从 python 生成 MATLAB 代码

    c - 获取用户信息函数

    java - 为什么这个 while 在接收值之前终止? (java)