简体   繁体   English

如何在 python 中为用户输入创建一个循环?

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

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.在我使用 python 的程序中,我正在给用户选择,只要做出这些选择是为了让程序运行良好,但是假设用户选择了第四个选项,然后想要 go 返回并执行第一个选项程序终止。 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.我使用 if 语句而不是 while 循环来处理不同的选择。 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'.为了继续运行程序,我在 while 循环中运行选项 function,直到用户选择“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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何进行验证循环以确保用户在 a.split 输入中输入正确的次数? - How do I make a validation loop to make sure the user inputs the correct amount of times in a .split input? 如何用用户输入制作矩形? - How do I make a rectangle with user input? 如何在Python中循环输入? - How Do I Loop An Input In Python? 如何使用户输入不是字符串或输入而是运算符? - How do I make user input not a string or input but an operator? 如何使用for循环和带用户输入的if语句在python字典中的键/值之间循环? - How do I cycle through keys/values in a python dictionary using a for loop and if statement with user input? 在Python中,如果输入有效,我如何使条件循环不执行? - In Python, How do I make my conditional if loop not execute if input is valid? python3 如果没有用户输入更新变量,我如何继续循环? - python3 How do I continue a loop if no user input to update variable? 在Python中,如果用户输入错误的值,如何使输入验证继续循环 - In Python, How do I make my input validation keep on looping if the user enters an incorrect value 如何让 python 在 def main() 中只要求用户输入一次? - How do I make python only ask for user input once in def main()? 如何让用户输入以在 django/python 中搜索内容? - How do I make a input from the user to search for something in django/python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM