Python: Unable to call function when in while loop

11,997

Solution 1

Okay, so you made a few mistakes (clearly), not a big deal, everyone's gotta start learning somewhere.

The biggest problem is you're getting into your menu loop (the second while loop you have), but never doing anything to exit it. There are a couple other changes that I commented as well. I wasn't 100% sure what you were going for in some places... but...

I think this is what you were going for though, I commented the changes. There were some weird thing that I just kind of left because I figured that was the intent.

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

Solution 2

The problem is that your code doesnt follow the flowpath that you want, try the code above and see if that's what you want, I'll think about it a little bit and try to explain what I did (for now I just created a function whileloop() and added it into the correct place).

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

I figured what happened. Ill list the flow that your code is going through and you might understand it in a simple way.

  1. Enters loop while validintro;
  2. Enters while True loop (chosen_option = menu())
  3. Enters menu() and calls test1()
  4. Enters test1() and calls logged()
  5. Enters logged() and NOW is the thing. Your execution flow will return to when you called the test1() function in the While True loop.
  6. You enter the if chosen_option in ['b', 'B'].
  7. since it's not inside b,B you will activate your else statement and print your error message. After that, the loop restarts.
Share:
11,997
Shad342
Author by

Shad342

Updated on July 13, 2022

Comments

  • Shad342
    Shad342 almost 2 years

    Just starting out here soz. I've been trying to make a menu with several options (def logged():) by entering a number and it would jump to that function with a purpose. However, I cannot seem to call the designated functions with the if statements put in a while loop, and instead, it jumps back to the menu() function when the logged function is supposed to keep running through the while loop forever.

    When I enter the corresponding number in the menu of logged(), it should call that specific function, but it just jumps way back to the 1st menu. I just can't seem to get the two menus to loop forever without them jumping back and forth. So how exactly do I make two while loops loop forever separately and not into each other?

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