954,541 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Simple Dictionary Question

Hi, I have been learning Python and have a quick dictionary question.

I am trying to make a "vocabulary test" to help myself study, and am trying to do it with a dictionary. So far I have this:

testwords = {"uno":"one","dos":"two","tres":"three","cuatro":"four",\
             "cinco":"five"}

def vocabtest():
    for value in testwords:
        print "What word does the following definition correspond with?"
        print value
        answer = raw_input("> ")
        if answer == :
            print "Correct!"

vocabtest()


I need help with line 9, "if answer == :".
I am not sure how I would check to see if the user's answer is equal to the corresponding key.

Any help would be greatly appreciated. Thanks.

SoulMazer
Posting Whiz in Training
213 posts since Sep 2008
Reputation Points: 23
Solved Threads: 12
 

An appropriate variable name in the for loop would be key instead of value, because you are iterating on the keys of the dictionary.

if answer == testwords[key]:
solsteel
Junior Poster
145 posts since Mar 2007
Reputation Points: 86
Solved Threads: 42
 

I guess you want to write this

def vocabtest():
    for value in testwords:
        print "What word does the following definition correspond with?"
        print value
        answer = raw_input("> ")
        if (answer == testwords[value]):
          print "Correct!"
        else:
          print "No the answer is ",testwords[value]
StuXYZ
Practically a Master Poster
680 posts since Nov 2008
Reputation Points: 760
Solved Threads: 138
 

Thank you very much! Problem solved!

SoulMazer
Posting Whiz in Training
213 posts since Sep 2008
Reputation Points: 23
Solved Threads: 12
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You