如何使用用户输入创建字典和输出值?
How can I create a dictionary and output values using 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
试试这个:
dictionary={
"1":"Number one"
"2":"Number two"
"3":"Number three"}
x = raw_input()
print dictionary[str(x)]
试试这个
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)
我想制作一个字典,然后使用用户输入打印键的值
编辑:我正在使用 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
试试这个:
dictionary={
"1":"Number one"
"2":"Number two"
"3":"Number three"}
x = raw_input()
print dictionary[str(x)]
试试这个
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)