Search code examples
pythondictionaryoutputuser-input

How can I create a dictionary and output values using user input?


I want to make a dictionary and then use user input to print the value of the key

Edit: I'm using Python 3.4.4 And thanks to the user who corrected grammatical and syntactical errors

*I tried to use the -input()- but with no luck *I found this code:

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)

But it is not what I want. My goal is to do something like this:

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

#Asking for user input code here

print() #Print the value of the key here

Solution

  • Try this:

    dictionary={
     "1":"Number one"
     "2":"Number two"
     "3":"Number three"}
    
    x = raw_input()
    print dictionary[str(x)]