[Python] Why is the function being called twice?

Discussão em 'Python' iniciado por Stack, Julho 24, 2025.

  1. Stack

    Stack Membro Participativo

    Here's the code:

    truthful:

    cart = {}
    subMenuItems = {}
    import menu_2
    menu_2.mainmenu()


    Main:

    def mainmenu():
    ID = ("FB10", "FS20", "FD30", "FD40", "FC50")
    print(ID)
    choice = input("> ").upper().strip()
    while True: #vailidates input
    if choice not in ID:
    print("Please choose a valid selection")
    choice = input("> ").upper().strip()
    elif choice in ID:
    # print(f"You've selected {choice}")
    import sub_menu_5
    sub_menu_5.assign_sub_menu_items(choice)
    sub_menu_5.sub_menu()
    break


    Sub:


    def input_stuff():
    import truthful
    while True:
    userInput = input('> ').strip().upper()

    if userInput in truthful.subMenuItems:
    return userInput

    elif userInput == '1' or userInput == 'MAIN MENU':
    return userInput

    elif userInput == '2' or userInput == 'CART':
    return userInput

    else:
    print('Invalid input. PLease try again')

    def sub_menu():
    display_menu() #imports subMenuItems from truthful
    stuff = input_stuff()
    print(stuff)

    import cart_3,menu_2
    if stuff == '1' or stuff == 'MAIN MENU':
    print('Entering Main Menu')
    menu_2.mainmenu()
    elif stuff == '2' or stuff == 'CART':
    print('Entering Cart')
    cart_3.cart_display()
    valid = True
    while valid:
    try:
    cart_3.cart_input()
    valid = False
    except ValueError:
    print('Invalid input, try again.')
    else: ...






    def cart_input(): #does all the input stuff within the cart
    import truthful
    if truthful.cart != {}: #if cart not M.T.
    userInput = float(input('> ')) #we're not cheesing ValueError :)disappointed:)
    if userInput == 1:
    ...
    if userInput == 2:
    ...
    elif userInput == 3: #prints main menu. pls add menu function.
    import menu_2
    print('Entering Main Menu: ')
    menu_2.mainmenu()
    elif userInput == 4: #prints checkout. a
    checkout()
    else:
    print('Invalid input.')
    cart_input()
    else: #when cart from truthful is empty
    print('Enter anything to return to the Main Menu.')
    input('> ')
    import menu_2
    menu_2.mainmenu()

    def checkout():
    import truthful
    import menu_2
    import Discount
    ...
    while True:
    checkoutInput = input('> ').lower().strip()
    if checkoutInput == '1' or checkoutInput == 'checkout':
    print('Checkout complete!')
    print('Would you like to order again?')
    #if yes then k-
    return
    elif checkoutInput == '2' or checkoutInput == 'main menu':
    print('Entering Main Menu')
    menu_2.mainmenu()
    elif checkoutInput == '3' or checkoutInput == 'cart':
    print('Entering Cart')
    cart_display()
    finalValid = False
    while finalValid == False:
    try:
    cart_input()
    finalValid = True
    except ValueError:
    print('Invalid input, try again')
    else:
    print('Please enter a valid input.')


    When I input an ID for 'choice = input("> ")' for the first time, mainmenu() runs again.

    When I input 1 for checkoutInput, it runs the print statements within the if, and then runs subMenu().

    Continue reading...

Compartilhe esta Página