Search code examples
pythonloopsif-statementdictionaryraw-input

Accessing Dictionary with Raw Input


I am trying to have a user input a 1,2,3,4,5,6. Then have that integer align with a character name in my dictionary.

characters = {
'Stark': '1',
'Greyjoy': '2',
'California': '3',
'Whitewalkers': '4',
'Knights Watch': '5',
'Dalthraki': '6'
 }   

print 'Type 1 for Stark'
print 'Type 2 for Greyjoy'
print 'Type 3 for Lannister'
print 'Type 4 for Whitewalkers'
print 'Type 5 for Knights Watch'
print 'Type 6 for Dalthraki' 
choice = raw_input("> ")
if choice in characters:
    print 'You\'ve selected', choice 
else:
    splash()

I want to have my script print "You've selected Stark" after having the user input a 1. Thanks for your help


Solution

  • You have the dict backwards:

    characters = {
    '1': 'Stark',
    '2': 'Greyjoy',
    '3': 'California',
    '4': 'Whitewalkers',
    '5': 'Knights Watch',
    '6': 'Dalthraki',
    }   
    
    print 'Type 1 for Stark'
    print 'Type 2 for Greyjoy'
    print 'Type 3 for Lannister'
    print 'Type 4 for Whitewalkers'
    print 'Type 5 for Knights Watch'
    print 'Type 6 for Dalthraki' 
    choice = raw_input("> ")
    if choice in characters:
        print 'You\'ve selected', characters[choice] 
    else:
        pass