Simple Dictionary Question

Reply

Join Date: Sep 2008
Posts: 86
Reputation: SoulMazer is an unknown quantity at this point 
Solved Threads: 1
SoulMazer SoulMazer is offline Offline
Junior Poster in Training

Simple Dictionary Question

 
0
  #1
Dec 7th, 2008
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:
  1. testwords = {"uno":"one","dos":"two","tres":"three","cuatro":"four",\
  2. "cinco":"five"}
  3.  
  4. def vocabtest():
  5. for value in testwords:
  6. print "What word does the following definition correspond with?"
  7. print value
  8. answer = raw_input("> ")
  9. if answer == :
  10. print "Correct!"
  11.  
  12. 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.
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 110
Reputation: solsteel is on a distinguished road 
Solved Threads: 31
solsteel solsteel is offline Offline
Junior Poster

Re: Simple Dictionary Question

 
0
  #2
Dec 7th, 2008
An appropriate variable name in the for loop would be key instead of value, because you are iterating on the keys of the dictionary.
  1. if answer == testwords[key]:
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 392
Reputation: StuXYZ is a glorious beacon of light StuXYZ is a glorious beacon of light StuXYZ is a glorious beacon of light StuXYZ is a glorious beacon of light StuXYZ is a glorious beacon of light StuXYZ is a glorious beacon of light 
Solved Threads: 72
StuXYZ StuXYZ is offline Offline
Posting Whiz

Re: Simple Dictionary Question

 
1
  #3
Dec 7th, 2008
I guess you want to write this

  1. def vocabtest():
  2. for value in testwords:
  3. print "What word does the following definition correspond with?"
  4. print value
  5. answer = raw_input("> ")
  6. if (answer == testwords[value]):
  7. print "Correct!"
  8. else:
  9. print "No the answer is ",testwords[value]
experience is the most expensive way to learn anything
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 86
Reputation: SoulMazer is an unknown quantity at this point 
Solved Threads: 1
SoulMazer SoulMazer is offline Offline
Junior Poster in Training

Re: Simple Dictionary Question

 
0
  #4
Dec 7th, 2008
Thank you very much! Problem solved!
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Python Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC