943,840 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Unsolved
  • Views: 495
  • Python RSS
Dec 7th, 2008
0

Simple Dictionary Question

Expand Post »
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:
python Syntax (Toggle Plain Text)
  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.
Similar Threads
Reputation Points: 23
Solved Threads: 12
Posting Whiz in Training
SoulMazer is offline Offline
212 posts
since Sep 2008
Dec 7th, 2008
0

Re: Simple Dictionary Question

An appropriate variable name in the for loop would be key instead of value, because you are iterating on the keys of the dictionary.
Python Syntax (Toggle Plain Text)
  1. if answer == testwords[key]:
Reputation Points: 86
Solved Threads: 40
Junior Poster
solsteel is offline Offline
141 posts
since Mar 2007
Dec 7th, 2008
1

Re: Simple Dictionary Question

I guess you want to write this

Python Syntax (Toggle Plain Text)
  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]
Reputation Points: 732
Solved Threads: 134
Practically a Master Poster
StuXYZ is offline Offline
659 posts
since Nov 2008
Dec 7th, 2008
0

Re: Simple Dictionary Question

Thank you very much! Problem solved!
Reputation Points: 23
Solved Threads: 12
Posting Whiz in Training
SoulMazer is offline Offline
212 posts
since Sep 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: simple program help
Next Thread in Python Forum Timeline: **longest word help**





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC