[Tutor] Storing dictionary value, indexed by key, into a variable

John Aten welcome.to.eye.o.rama at gmail.com
Sat Apr 5 21:53:56 CEST 2014


I read the article on data driven programming that Danny linked too, and did some additional looking around. I couldn't find anything directly using Python, but I got an idea of the concept and went crazy with it. This may still be off the mark, but I created a complex combination of lists and dictionaries to represent each individual instance of each demonstrative (starting only with one): 

that_those = [ [ [ {'nom': 'ille', 'clue': 'That/Those, Singular, Masculine Nominative'},
				{'gen': 'illīus', 'clue': 'That/Those, Singular, Masculine Genitive'},
				{'dat': 'illī', 'clue': 'That/Those, Singular, Masculine Dative'},
				{'acc': 'illum', 'clue': 'That/Those, Singular, Masculine Accusative'},
				{'abl': 'illō', 'clue': 'That/Those, Singular, Masculine Ablative'} ], [{'nom': 'illī', 'clue': 'That/Those, Plural, Masculine Nominative'},
                {'gen': 'illōrum', 'clue': 'That/Those, Plural, Masculine Genitive'},
	     		{'dat': 'illīs', 'clue': 'That/Those, Plural, Masculine Dative'},
		     	{'acc': 'illōs', 'clue': 'That/Those, Plural, Masculine Accusative'},
	     		{'abl': 'illīs', 'clue': 'That/Those, Plural, Masculine Ablative'} ] ], [ [ {'nom': 'illa', 'clue': 'That/Those, Singular, Feminine Nominative'},
                {'gen': 'illīus', 'clue': 'That/Those, Singular, Feminine Genitive'},
	     		{'dat': 'illī', 'clue': 'That/Those, Singular, Feminine Dative'},
		     	{'acc': 'illam', 'clue': 'That/Those, Singular, Feminine Accusative'},
				{'abl': 'illā', 'clue': 'That/Those, Singular, Feminine Ablative'} ], [ {'nom': 'illae', 'clue': 'That/Those, Plural, Feminine Nominative'},
				{'gen': 'illārum', 'clue': 'That/Those, Plural, Feminine Genitive'},
	     		{'dat': 'illīs', 'clue': 'That/Those, Plural, Feminine Dative'},
				{'acc': 'illās', 'clue': 'That/Those, Plural, Feminine Accusative'},
	     		{'abl': 'illīs', 'clue': 'That/Those, Plural, Feminine Ablative'} ] ]
, [ [ {'nom': 'illud', 'clue': 'That/Those, Singular, Neuter Nominative'},
                {'gen': 'illīus', 'clue': 'That/Those, Singular, Neuter Genitive'},
	     		{'dat': 'illī', 'clue': 'That/Those, Singular, Neuter Dative'},
		     	{'acc': 'illud', 'clue': 'That/Those, Singular, Neuter Accusative'},
	     		{'abl': 'illō', 'clue': 'That/Those, Singular, Neuter Ablative'} ], [ {'nom': 'illa', 'clue': 'That/Those, Plural, Neuter Nominative'},
                {'gen': 'illōrum', 'clue': 'That/Those, Plural, Neuter Genitive'},
	     		{'dat': 'illīs', 'clue': 'That/Those, Plural, Neuter Dative'},
		     	{'acc': 'illa', 'clue': 'That/Those, Plural, Neuter Accusative'},
	     		{'abl': 'illīs', 'clue': 'That/Those, Plural, Neuter Ablative'} ] ] ]

Now, this is a big mess, for sure, but it seems possible that it is abstract enough that I could reuse the same logic and just switch out the data to make different types of similar programs. Also, I can write a script to populate this structure to construct a program to drill any type of word that is declined by gender, number and case. I can call each item pretty easily like this: 

	question_to_be_dispalyed_to_user = that_those[gender][number][q_and_a]["clue"]
	answer_to_that_question = that_those[gender][number][q_and_a][case]

Where gender, number, q_and_a, and case follow Peter's suggestion:

	cases = ['nom', 'gen', 'dat', 'acc', 'abl']
	case = random.choice(cases)

The complete code will follow, but I have a spooky new problem. When I try to run the following code, I intermittently get the following error:

Traceback (most recent call last):
  File "./latDemTest.py", line 59, in <module>
    answer = that_those[gender][number][q_and_a][case]
KeyError: 'abl'

I looked this error up online, and it seems the key error is generated when one attempts to retrieve a value from a dictionary with a key that does not exist. The problem is, it seems to me that the keys should in fact exist. The odd thing is, the code returns this error sometimes, and sometimes it doesn't. I did a bunch of trials, keeping track of which particular entries in the data structures worked and which failed, and I discovered that they overlap. The dictionary  {'gen': 'illōrum', 'clue': 'That/Those, Plural, Neuter Genitive'} for example, happily spits out the proper question and answer sometimes, other times it shoots out the previously mentioned error. I cannot understand how this could happen! 


THE FULL CODE: 

#!/Library/Frameworks/Python.framework/Versions/3.2/bin/python3.2
#  Don't forget ./ before filename to run from cmnd line!

import random
import os 

that_those = [ [ [ {'nom': 'ille', 'clue': 'That/Those, Singular, Masculine Nominative'},
				{'gen': 'illīus', 'clue': 'That/Those, Singular, Masculine Genitive'},
				{'dat': 'illī', 'clue': 'That/Those, Singular, Masculine Dative'},
				{'acc': 'illum', 'clue': 'That/Those, Singular, Masculine Accusative'},
				{'abl': 'illō', 'clue': 'That/Those, Singular, Masculine Ablative'} ], [{'nom': 'illī', 'clue': 'That/Those, Plural, Masculine Nominative'},
                {'gen': 'illōrum', 'clue': 'That/Those, Plural, Masculine Genitive'},
	     		{'dat': 'illīs', 'clue': 'That/Those, Plural, Masculine Dative'},
		     	{'acc': 'illōs', 'clue': 'That/Those, Plural, Masculine Accusative'},
	     		{'abl': 'illīs', 'clue': 'That/Those, Plural, Masculine Ablative'} ] ], [ [ {'nom': 'illa', 'clue': 'That/Those, Singular, Feminine Nominative'},
                {'gen': 'illīus', 'clue': 'That/Those, Singular, Feminine Genitive'},
	     		{'dat': 'illī', 'clue': 'That/Those, Singular, Feminine Dative'},
		     	{'acc': 'illam', 'clue': 'That/Those, Singular, Feminine Accusative'},
				{'abl': 'illā', 'clue': 'That/Those, Singular, Feminine Ablative'} ], [ {'nom': 'illae', 'clue': 'That/Those, Plural, Feminine Nominative'},
				{'gen': 'illārum', 'clue': 'That/Those, Plural, Feminine Genitive'},
	     		{'dat': 'illīs', 'clue': 'That/Those, Plural, Feminine Dative'},
				{'acc': 'illās', 'clue': 'That/Those, Plural, Feminine Accusative'},
	     		{'abl': 'illīs', 'clue': 'That/Those, Plural, Feminine Ablative'} ] ]
, [ [ {'nom': 'illud', 'clue': 'That/Those, Singular, Neuter Nominative'},
                {'gen': 'illīus', 'clue': 'That/Those, Singular, Neuter Genitive'},
	     		{'dat': 'illī', 'clue': 'That/Those, Singular, Neuter Dative'},
		     	{'acc': 'illud', 'clue': 'That/Those, Singular, Neuter Accusative'},
	     		{'abl': 'illō', 'clue': 'That/Those, Singular, Neuter Ablative'} ], [ {'nom': 'illa', 'clue': 'That/Those, Plural, Neuter Nominative'},
                {'gen': 'illōrum', 'clue': 'That/Those, Plural, Neuter Genitive'},
	     		{'dat': 'illīs', 'clue': 'That/Those, Plural, Neuter Dative'},
		     	{'acc': 'illa', 'clue': 'That/Those, Plural, Neuter Accusative'},
	     		{'abl': 'illīs', 'clue': 'That/Those, Plural, Neuter Ablative'} ] ] ]

# place one: 0 = masculine, 1 = feminine, 2 = neuter
# place two: 0 = singular, 1 = plural
# place two: 
# place three: 0 = answer, 1 = clue
# place four, 'nom' = answer, 'clue' = description
# So, word[0][0][0]["nom"] should produce: ille

numbers = [0,1]
genders = [0,1,2]
cases = ['nom', 'gen', 'dat', 'acc', 'abl']

guess = ''
score = 0
tries = 0

while guess != 'exit':

	os.system('clear')

	q_and_a = random.choice(numbers)
	gender = random.choice(genders)
	number = random.choice(numbers)
	case = random.choice(cases)
	print(case)
	question = that_those[gender][number][q_and_a]["clue"]
	print(question)
	answer = that_those[gender][number][q_and_a][case]

	print(question)
	guess = input()
	if guess == 'exit':
		N = 0
		percentage = int((score/tries) * 100)
		print("You scored", score, "out of", tries, ", or", percentage, "%")

	tries +=1 
	
	if guess == answer:
		score += 1
		print('faster!')
	else:
		print(answer)
		print('slower...')

	input()



More information about the Tutor mailing list