-1

How can i go back to my main menu. Here is my code. Its not yet done. For example if I chose 1 how can I go back to my main menu without terminating the program.

done = False
while not done:
    print('=========================================')
    print('  Aria\'s Pet Store Point of Sale System')
    print('=========================================')
    print('[1] Add Products to Cart')
    print('[2] Remove Products to Cart')
    print('[3] View Cart')
    print('[4] Generate Reciept')
    print('[5] Logout')
    print('[6] Exit')
    print('=========================================')
    choice = input('Choice: ')

if choice == '1':
    pass
elif choice == '2':
    pass
elif choice == '3':
    pass
elif choice == '4':
    pass
elif choice == '5':
    pass
elif choice == '6':
    exit(0)
else:
    print('Invalid Input')

5 Answers5

1

Just simplify your code and check the conditions in the loop:

done = False
while not done:
    print('=========================================')
    print('  Aria\'s Pet Store Point of Sale System')
    print('=========================================')
    print('[1] Add Products to Cart')
    print('[2] Remove Products to Cart')
    print('[3] View Cart')
    print('[4] Generate Reciept')
    print('[5] Logout')
    print('[6] Exit')
    print('=========================================')
    choice = input('Choice: ')

    if choice == '1':
        pass
    elif choice == '2':
        pass
    elif choice == '3':
        pass
    elif choice == '4':
        pass
    elif choice == '5':
        pass
    elif choice == '6':
        exit(0)
    else:
        print('Invalid Input')
Code Pope
  • 5,075
  • 8
  • 26
  • 68
0

Your code by default goes back to the top of the loop. What is it that you want to achieve? Maybe you want to set done to True once some statements are satisfied and you want to get out of the loop, otherwise it will repeat your loop indefinitely. Maybe you meant to put if statement inside your loop:

done = False
while not done:
    print('=========================================')
    print('  Aria\'s Pet Store Point of Sale System')
    print('=========================================')
    print('[1] Add Products to Cart')
    print('[2] Remove Products to Cart')
    print('[3] View Cart')
    print('[4] Generate Reciept')
    print('[5] Logout')
    print('[6] Exit')
    print('=========================================')
    choice = input('Choice: ')

    if choice == '1':
        #do some stuff
    elif choice == '2':
        pass
    elif choice == '3':
        pass
    elif choice == '4':
        pass
    elif choice == '5':
        pass
    elif choice == '6':
        exit(0)
    else:
        print('Invalid Input')

    print('Would you like to choose again?')
    print('[1] Press y to go back to main menu')
    print('[2] press any other key to exit')
    redo = input('redo?: ')
    if redo != 'y':
        done = True
Ehsan
  • 12,072
  • 2
  • 20
  • 33
  • Yeah Iorgot to edit the if statement when im creating this but in my text editor it was properly indented. What am planning to do is that when I pick or enter 1 there will be another menu like and going back to the main menu is one of the choices. Thanks for answering btw – Arjay Abrenica May 19 '20 at 10:48
  • @ArjayAbrenica Please find the update in my post for your purpose. And welcome to SO. – Ehsan May 19 '20 at 17:10
0

You can push your if-else logic inside the indents. Also, you might want to use while True: to loop back into the menu; this will help you avoid redundant logic. Here's the refactored code.

while True:
    print('=========================================')
    print('  Aria\'s Pet Store Point of Sale System')
    print('=========================================')
    print('[1] Add Products to Cart')
    print('[2] Remove Products to Cart')
    print('[3] View Cart')
    print('[4] Generate Reciept')
    print('[5] Logout')
    print('[6] Exit')
    print('=========================================')
    choice = input('Choice: ')

    if choice == '1':
        pass
    elif choice == '2':
        pass
    elif choice == '3':
        pass
    elif choice == '4':
        pass
    elif choice == '5':
        pass
    elif choice == '6':
        exit(0)
    else:
        print('Invalid Input')
Redowan Delowar
  • 1,580
  • 1
  • 14
  • 36
0

Your if segment is out of the while loop, so that if segment can't read the value of "choice" variable. If you move them in the while loop, it will work properly.

rknksy
  • 21
  • 5
  • When i wrote it, there were no answers. Is that helpful now for you? – rknksy May 19 '20 at 10:37
  • I forgot to edit my if segment here but in my text editor it was properly indented. What am planning to do is that when I pick or enter 1 there will be another menu-like and going back to the main menu is one of the choices. Thanks for answering btw. – Arjay Abrenica May 19 '20 at 10:54
0

According to your additional explanation i fixed it. You should use "function"(https://www.w3schools.com/python/python_functions.asp) at repeating task. And also you can modify the menu options as i made at first option.

def mainmenu(A_Cart):
    print('=========================================')
    print('  Aria\'s Pet Store Point of Sale System')
    print('=========================================')
    print(f'[1] {A_Cart}')
    print('[2] Remove Products to Cart')
    print('[3] View Cart')
    print('[4] Generate Reciept')
    print('[5] Logout')
    print('[6] Exit')
    print('=========================================')
    choice = input('Choice: ')
    return choice


done = False
while not done:
    choice = mainmenu('Add Products to Cart')
    if choice == '1':
        mainmenu('Murda - Eh Baba (prod. Rockywhereyoubeen)')
    elif choice == '2':
        pass
    elif choice == '3':
        pass
    elif choice == '4':
        pass
    elif choice == '5':
        pass
    elif choice == '6':
        exit(0)
    else:
        print('Invalid Input')
rknksy
  • 21
  • 5