使用用户输入来填充字典

 

>>> tag = True
>>> test1 = {}
>>> type(test1)
<class 'dict'>
>>> while tag:
    name = input("please input your name: ")
    mountain = input("which mountain to you want to climb next sunday ? ")
    test1[name] = mountain
    repeat = input("would you like another person to attend poll? yes or no?")
    if repeat == "no":
        tag = False

        
please input your name: zhangsan
which mountain to you want to climb next sunday ? taishan
would you like another person to attend poll? yes or no?yes
please input your name: lisi
which mountain to you want to climb next sunday ? songshan
would you like another person to attend poll? yes or no?yes
please input your name: wangwu
which mountain to you want to climb next sunday ? huashan
would you like another person to attend poll? yes or no?no
>>> test1
{'zhangsan': 'taishan', 'lisi': 'songshan', 'wangwu': 'huashan'}
>>> for i,j in test1.items():
    print(i,"",j)

    
zhangsan → taishan
lisi → songshan
wangwu → huashan

 

posted @ 2021-01-02 19:46  小鲨鱼2018  阅读(114)  评论(0编辑  收藏  举报