简体   繁体   English

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

[英]python version 3: how to ask user to add item to dictionary and print a message that updates with their input?

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.打印出比萨包含的所有配料,例如:此比萨包含配料 1、配料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)

First, I dont understanding why are you using a while loop.首先,我不明白你为什么使用 while 循环。 Since the program will exit the while loop as long as the user Enter something.因为只要用户输入一些东西,程序就会退出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=" ")

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM