0

I am trying to make the main menu text keep repeating until the user chooses 5 and then the program closes but I don't know how to make it keep repeating. I tried to use a while loop which didn't work and started spamming the main menu. So here is the code all I need is for you to explain how to make the main menu repeat and the choice repeat until the user picks option five (exit).

print('''------------
Main Menu
------------''')
one='1 - show all contacts'
two='2 - add a new contact'
three='3 - find contact'
four='4 - Delete contact'
five='5 - Quit'
print(one)
print(two)
print(three)
print(four)
print(five)

choice=input('enter your chouse : ')
 
if choice == '1':
 print('this is your list : ')
    
elif choice == '2':
     print('add your contact : ')

elif choice == '3':
    print('type in a contact you would like to find')

elif choice == '4':
    print('type in a contact you would like to delete')

elif choice == '5':
 print('quitting....')
 quit()

while choice != '5' :
    print('''------------
Main Menu
------------''')
one='1 - show all contacts'
two='2 - add a new contact'
three='3 - find contact'
four='4 - Delete contact'
five='5 - Quit'
print(one)
print(two)
print(three)
print(four)
print(five)

choice=input('enter your chouse : ')
 
if choice == '1':
 print('this is your list : ')
    
elif choice == '2':
     print('add your contact : ')

elif choice == '3':
    print('type in a contact you would like to find')

elif choice == '4':
    print('type in a contact you would like to delete')

elif choice == '5':
 print('quitting....')
 quit()

I tried to use a while loop which didn't work and started spamming the main menu.

quamrana
  • 37,849
  • 12
  • 53
  • 71

1 Answers1

0

As others have said in the comments, you have the right idea to use a while loop, but the loop needs to contain both

  1. The user prompt for input
  2. the handling of that input

Here's one possible solution. I moved the printing of the menu to a function using a single multi-line string print command. The loop will then call this function on every iteration. I also replaced the call to quit() with break to exit the loop, or you could replace it with sys.exit(), which is the better option in a script, whereas quit() and exit() are for the interactive shell.

import sys

def printMenu():
    # Move this to a function so the menu is only in one place
    print('''------------
    Main Menu
    ------------
    1 - show all contacts
    2 - add a new contact
    3 - find contact
    4 - Delete contact
    5 - Quit
    ''')

while True:
    printMenu()
    choice=input('enter your choice : ')
 
    if choice == '1':
       print('this is your list : ')
    
    elif choice == '2':
       print('add your contact : ')

    elif choice == '3':
        print('type in a contact you would like to find')

    elif choice == '4':
        print('type in a contact you would like to delete')

    elif choice == '5':
        print('quitting....')
        break   # Exits the loop, or use sys.exit() to exit the script immediately

There are a number of ways this could be enhanced, but this should get you on the right track.

nigh_anxiety
  • 1,428
  • 2
  • 4
  • 12