In my program using python, I'm giving the user choices and as long as those choices are made in order the program works great, but lets say the user chooses the 4th option, and then wants to go back and do the first option the program terminates. Here is the code for my program can anyone suggest how to make this work?
### start of program loop
def script():
### Welcome message
un = input('What is your name?\n')
print('Welcome to the Pizza Party Maker', un, '!\n')
print('This will create a guest list, with the items your guests are bringing to the party, and what pizza topping they want!\n')
### create dictionary
guests = {}
### input choice
choice = int(input('What would you like to do (1) Add a guest , (2) Remove a guest, (3) Update a guest, (4) See List, (5) Make a new list, (6) Exit\n'))
### add guest, item, and pizza topping to dictionary
while choice == 1:
gn = input('What is your guest''s name?\n')
print('What is', gn, 'bringing to the party?(salads, desserts, drinks, plates etc.)')
gi = input()
print('What pizza topping would', gn, 'like to have on his/her pizza?(Pepperoni, Cheese, Mushrooms, Onions, etc.)')
gt = input()
print()
guests[gn]=gi,gt
choice = int(input('What would you like to do (1) Add a guest , (2) Remove a guest, (3) Update a guest, (4) See List, (5) Make a new list, (6) Exit\n'))
### remove guest
while choice == 2:
gr = input('Which guest would you like to remove?\n')
del guests[gr]
print(gr, 'has been removed from your party!\n')
choice = int(input('What would you like to do (1) Add a guest , (2) Remove a guest, (3) Update a guest, (4) See List, (5) Make a new list, (6) Exit\n'))
### edit guest
while choice == 3:
gn = input('Which guest would you like to update?\n')
print('What is', gn, 'bringing to the party instead?(salads, desserts, drinks, plates etc.)')
gi = input()
print('What pizza topping would', gn, 'like to have on his/her pizza instead?(Pepperoni, Cheese, Mushrooms, Onions, etc.)')
gt = input()
print()
guests[gn]=gi,gt
choice = int(input('What would you like to do (1) Add a guest , (2) Remove a guest, (3) Update a guest, (4) See List, (5) Make a new list, (6) Exit\n'))
### show guest list (sorted alphabetical)
while choice == 4:
print('This is your guest list, what they are bringing, and what topping they want!')
for i in sorted (guests):
print((i, guests[i]), end = " ")
print()
choice = int(input('What would you like to do (1) Add a guest , (2) Remove a guest, (3) Update a guest, (4) See List, (5) Make a new list, (6) Exit\n'))
### Loop back to the beginning
while choice == 5:
if choice == 5:
script()
### End program
while choice == 6:
print('Thank you for using Mike\'s Pizza Party Maker!')
return
### not a valid choice
else:
print('That is an invalid choice, please try again.\n')
choice = int(input('What would you like to do (1) Add a guest , (2) Remove a guest, (3) Update a guest, (4) See List, (5) Make a new list, (6) Exit\n'))
script()
You could use a different setup and important is to add an uption to handle the situation where someone removes a guest that isn't on your list. I use if-statements instead of while-loops for handling the different choices. This makes more sense to me. To keep running the program, I'm running the choice function in a while-loop until the user chooses '6'.
### start of program loop
def initialize_script():
### Welcome message
un = input('What is your name?\n')
print('Welcome to the Pizza Party Maker', un, '!\n')
print('This will create a guest list, with the items your guests are bringing to the party, and what pizza topping they want!\n')
### create dictionary
guests = {}
return guests
def choice_script(guests):
### input choice
choice = int(input('What would you like to do (1) Add a guest , (2) Remove a guest, (3) Update a guest, (4) See List, (5) Make a new list, (6) Exit\n'))
exit=False
### add guest, item, and pizza topping to dictionary
if choice == 1:
gn = input('What is your guest''s name?\n')
print('What is', gn, 'bringing to the party?(salads, desserts, drinks, plates etc.)')
gi = input()
print('What pizza topping would', gn, 'like to have on his/her pizza?(Pepperoni, Cheese, Mushrooms, Onions, etc.)')
gt = input()
print()
guests[gn]=gi,gt
### remove guest
elif choice == 2:
gr = input('Which guest would you like to remove?\n')
try:
del guests[gr]
print(gr, 'has been removed from your party!\n')
except KeyError:
print(f'{gr} is not in your party')
### edit guest
elif choice == 3:
gn = input('Which guest would you like to update?\n')
print('What is', gn, 'bringing to the party instead?(salads, desserts, drinks, plates etc.)')
gi = input()
print('What pizza topping would', gn, 'like to have on his/her pizza instead?(Pepperoni, Cheese, Mushrooms, Onions, etc.)')
gt = input()
print()
guests[gn]=gi,gt
### show guest list (sorted alphabetical)
elif choice == 4:
print('This is your guest list, what they are bringing, and what topping they want!')
for i in sorted (guests):
print((i, guests[i]), end = " ")
print()
### Loop back to the beginning
elif choice == 5:
if choice == 5:
initialize_script()
### End program
elif choice == 6:
print('Thank you for using Mike\'s Pizza Party Maker!')
exit = True
### not a valid choice
else:
print('That is an invalid choice, please try again.\n')
choice = int(input('What would you like to do (1) Add a guest , (2) Remove a guest, (3) Update a guest, (4) See List, (5) Make a new list, (6) Exit\n'))
return guests, exit
guests = initialize_script()
exit = False
while not exit:
guests, exit = choice_script(guests)
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments