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

时间:2017-06-21 12:26:07

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

刚开始这里soz。我一直试图通过输入一个数字来创建一个包含多个选项($this->db->select('table1.item1 FROM table1'); $this->db->from('table1'); $this->db->join('base2.table3', 'base2.table3.item2 =table1.item2'); $this->where('base2.table3.item4','toto') $query = $this->db->get(); :)的菜单,它会跳转到该功能。但是,我似乎无法调用指定的函数,if语句放在while循环中,而是当记录的函数应该永远在while循环中运行时,它会跳回def logged()函数。 / p>

当我在menu()的菜单中输入相应的数字时,它应该调用该特定功能,但它只是跳回到第一个菜单。我似乎无法让两个菜单永远循环而不会来回跳跃。那么我究竟是如何让两个while循环永远分开而不是相互循环呢?

logged()

2 个答案:

答案 0 :(得分: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 :(得分: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语句并打印你的错误信息。之后,循环重新开始。