| | |
Simple Dictionary Question
Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Sep 2008
Posts: 90
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: 397
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 |
accessdenied advanced application argv beginner change color command convert csv cursor def dictionary digital dynamic dynamically edit editing enter event examples excel file float format frange function google gui homework i/o import input jaunty java keyboard lapse line linux list lists loop microphone mouse movingimageswithpygame newb number numbers numeric obexftp output parameters parsing path port prime programming projects py2exe pygame pygtk pyopengl python random recursion remote return reverse scrolledtext session simple skinning smtp sprite stderr string strings subprocess syntax table tennis terminal text thread threading time tkinter tlapse tuple tutorial ubuntu unicode unit urllib urllib2 variable voip web-scrape windows wxpython





