python版本3:如何要求用户将项目添加到字典并打印一条用其输入更新的消息?

时间:2022-12-28 15:45:06

I'm a beginner learning Python and I'm stuck on this problem:

我是初学者学习Python而且我坚持这个问题:

Ask the user to add one topping to the pizza. Modify the dictionary to have that item. Print out every topping the pizza contains, such as: This pizza contains Topping 1,Topping 2... and include the topping that the user added.

要求用户在披萨上添加一个顶部。修改字典以包含该项目。打印披萨所包含的每一个顶部,例如:这个披萨包含Topping 1,Topping 2 ...并包括用户添加的顶部。

Given example of dictionary:

给出字典的例子:

toppings = {‘pepperoni’:True,’olives':False,'pineapple':True,
‘sausage’:False,’jalapenos’:False}

This is what I have so far for the solution:

这是我迄今为止的解决方案:

toppings= {'pepperoni':True, 'olives':False,'pineapple':True,
'sausage':False, 'jalapenos': False}

while True:
    pizza = input("Add one topping to the pizza:" )
    toppings[pizza] = input
    break

    print ("You ordered a pizza with the following toppings: " +  [toppings])

for toppings in pizza ['toppings']:
    print("\t" + topping)

1 个解决方案

#1


0  

First, I dont understanding why are you using a while loop. Since the program will exit the while loop as long as the user Enter something.

首先,我不明白为什么你使用while循环。因为只要用户输入内容,程序就会退出while循环。

This is a way doing it

这是一种方式

toppings= {'pepperoni':True, 'olives':False,'pineapple':True,
'sausage':False, 'jalapenos': False}

pizza = input("Add one topping to the pizza:" )
toppings[pizza] = True

print("This pizza contains ",end="")
for topping, topping_status in toppings.items():
    if topping_status:
        print(topping,end=" ")

#1


0  

First, I dont understanding why are you using a while loop. Since the program will exit the while loop as long as the user Enter something.

首先,我不明白为什么你使用while循环。因为只要用户输入内容,程序就会退出while循环。

This is a way doing it

这是一种方式

toppings= {'pepperoni':True, 'olives':False,'pineapple':True,
'sausage':False, 'jalapenos': False}

pizza = input("Add one topping to the pizza:" )
toppings[pizza] = True

print("This pizza contains ",end="")
for topping, topping_status in toppings.items():
    if topping_status:
        print(topping,end=" ")