Python使用用户输入来填充字典

可以使用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)

输出结果:

发布了61 篇原创文章 · 获赞 35 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/qq_41575507/article/details/95893007