可以使用while循环来不断的向字典中填充内容,比如手机店卖手机需要展示手机的价格。我们将手机的品牌作为字典的键,手机的价格作为字典的值。
phones = {}
polling_active = True
while polling_active:
phone_name = input("请输入手机的品牌:")
price = input("请输入手机的价格:")
#将手机的品牌及价格存入字典中
phones[phone_name] = price
#判断是否继续输入
repeat = input("是否继续输入?(yes/no)")
if repeat == 'no':
polling_active = False
print(phones)
输出结果: