如何使用用户输入创建字典和输出值?

时间:2016-02-21 06:22:26

标签: python dictionary output user-input

我想创建一个字典,然后使用用户输入来打印密钥的值

编辑:我使用的是Python 3.4.4 并且感谢纠正语法和语法错误的用户

*我尝试使用-input() - 但没有运气 *我找到了这段代码:

usrInp = 'z' #default value z so that while works first time
exDict = {}  #initializing empty dict

while usrInp == 'z':
    key = input('enter key : ')
    val = input('enter val : ')
    exDict[key] = val
    usrInp = input('Enter z to continue adding, else any input to exit : ')

print(exDict)

但这不是我想要的。我的目标是做这样的事情:

 dictionary{
     1:"Number one"
     2:"Number two"
     3:"Number three"
}

#Asking for user input code here

print() #Print the value of the key here

2 个答案:

答案 0 :(得分:0)

试试这个:

git push

答案 1 :(得分:0)

试试这个



    usrInp = 'z' #default value z so that while works first time
    exDict = {}  #initializing empty dict
    while usrInp == 'z':
        key = input('enter key : ')) 
        val = input('enter val : ')) 
        exDict[key] = val
        usrInp = input('Enter z to continue adding, else any input to exit : ')

    print(exDict)