| | |
Simple Dictionary Question
![]() |
•
•
Join Date: Sep 2008
Posts: 86
Reputation:
Solved Threads: 1
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:
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.
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)
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.
•
•
Join Date: Mar 2007
Posts: 110
Reputation:
Solved Threads: 31
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)
if answer == testwords[key]:
•
•
Join Date: Nov 2008
Posts: 392
Reputation:
Solved Threads: 72
I guess you want to write this
Python Syntax (Toggle Plain Text)
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]
experience is the most expensive way to learn anything
![]() |
Similar Threads
- Projects for the Beginner (Python)
- Hacking Question? (IT Professionals' Lounge)
- Effiency in list searching (Python)
- Improve scrambled text game (Python)
- Help with automatic update problem and more (Viruses, Spyware and other Nasties)
- Total noob question (Database Design)
- Refreshing a Tkinter Canvas Multiple Times: (Python)
- Yellow flashing question mark/ "X".....GRRRRRRR (Viruses, Spyware and other Nasties)
Other Threads in the Python Forum
- Previous Thread: simple program help
- Next Thread: **longest word help**
| Thread Tools | Search this Thread |
abrupt accessdenied anti apache application approximation argv array beginner book builtin calculator change converter countpasswordentry curved dan08 dictionaries dictionary dynamic edit enter examples file float format function gui homework import inches input java keyboard lapse launcher library line lines linux list lists loop microphone mouse movingimageswithpygame mysqlquery newb number numbers numeric output parameters parsing path phonebook plugin port prime programming progressbar projects py2exe pygame pyopengl python random recursion redirect remote reverse scrolledtext session simple software sprite statictext string strings syntax table terminal text textarea threading time tlapse trick tricks tuple tutorial twoup ubuntu unicode unit urllib urllib2 variable wordgame wxpython





