How do I make a loop for user input in python?

Michael Bernard

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()
Niels Henkens

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.

edited at
0

Comments

0 comments
Login to comment

Related

How do I validate user input in a loop?

How do I make a rectangle with user input?

how do i make the user input a double?

How do I use a for loop to make multiple similar arrays based on user input?

How do I make a for loop ask a user for the same input again after an Exception?

How do I make a loop until the user gives correct input in Java, if there's multiple 'correct' paths?

How do I make a loop until the user gives correct input in Java, if there's multiple 'correct' paths?

How do i validate a user input in python?

How do i make this pattern from user input using a for loop (i want my code to be able to produce the output on the bottom)

Python Beginner (need assistance) - How do I make a While loop only accept certain user inputs?

How do I get a user input inside a `for in` loop

How do I return the user input value inside of a while loop?

How do i use user input for a string array that is in a while loop?

How do I break the while loop with user input

How do I loop a function after receiving user input?

How do I exit a for or a while loop with user input in java

How do I make a program that reverse user input of integers in Java?

How do I make this user input change the calculation in this if statement?

How do I make my nextLine() wait for the user's input?

How do I make the user input reflect in the new list created?

How do I make a function that googles the input from the user, with while loop unless I type 'exit' which breaks it?Do I need to define the variable?

How do I make this a loop?

How do I make my variable have a default value "0" when no input has been provided by the user in python?

Python 3: How do I make a game using user input and retrieving answer from a list of dictionaries, then use a point system?

How do I get user input from discord chat in python?

How do I get the user to input a number in Python 3?

How do I gather user numerical input into a list in Python?

Python- how do I write user input to an Excel file?

How do I check user input against multiple lists python?