簡體   English   中英

如何在 python 中為用戶輸入創建一個循環?

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

在我使用 python 的程序中,我正在給用戶選擇,只要做出這些選擇是為了讓程序運行良好,但是假設用戶選擇了第四個選項,然后想要 go 返回並執行第一個選項程序終止。 這是我的程序的代碼,任何人都可以建議如何使它工作嗎?

### 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()

您可以使用不同的設置,重要的是添加一個選項來處理有人刪除不在您列表中的客人的情況。 我使用 if 語句而不是 while 循環來處理不同的選擇。 這對我來說更有意義。 為了繼續運行程序,我在 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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM