0

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()
1
  • Wrap the bits you want repeated in a function, and repeat that function call using choice as the function's parameter. Have the function run in a while loop or something until input = 'q' or not an int (i.e. choice). Commented Feb 3, 2021 at 21:22

1 Answer 1

1

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)
Sign up to request clarification or add additional context in comments.

7 Comments

I think this puts me on the right track, however when i run the program now it stops prior to asking what i want to do. thanks for the help I'll keep at it.
Where does it stop? It seems to work fine for me.
Do you get an error message, or does it terminate normally? Does it enter the while-loop?
I copy and paste yours and it works fine, so must be something I messed up
Traceback (most recent call last): File "C:/Users/jaxfi/OneDrive/Desktop/COP1000/Pizza_party_test.py", line 81, in <module> guests, exit = choice_script(guests) NameError: name 'choice_script' is not defined
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.