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

John Aten welcome.to.eye.o.rama at gmail.com
Thu Apr 3 20:14:24 CEST 2014


I apologize for the omissions, I thought that I had isolated the problem, but I was way off the mark. The problem was, as suggested by Danny and Peter, in the function where the dictionary is assigned. I ran the type function, as Alex advised, and lo and behold the function was returning a string. In researching this, I learned that a function can return multiple values, so I expanded things a bit. Now, the function that randomly selects which demonstrative to drill also selects which a string to present as the clue (the first part, anyway), and returns the dictionary and the first part of the clue in a tuple. I then unpack that tuple into variables and work with those. 

The thing is, this looks really messy Could anyone give me some pointers on how this could be more elegantly done?

Here's the full code:
import random
import os

# ille, that/those masculine
that_those_Masculine_Singular = {'nom': 'ille', 'gen': 'illīus', 'dat': 'illī', 'acc': 'illum', 'abl': 'illō'}
that_those_Masculine_Plural = {'nom': 'illī', 'gen': 'illōrum', 'dat': 'illīs', 'acc': 'illōs', 'abl': 'illīs'}

# the rest of the dictionaries are empty. I wanted to get it working before putting everything in. 
# ille, that/those feminine
that_those_Feminine_Singular = {'nom': '', 'gen': '', 'dat': '', 'acc': '', 'abl': ''}
that_those_Feminine_Plural = {'nom': '', 'gen': '', 'dat': '', 'acc': '', 'abl': ''}

# ille, that/those neuter
that_those_Neuter_Singular = {'nom': '', 'gen': '', 'dat': '', 'acc': '', 'abl': ''}
that_those_Neuter_Plural = {'nom': '', 'gen': '', 'dat': '', 'acc': '', 'abl': ''}

# hic, this/these masculine
this_these_Masculine_Singular = {'nom': '', 'gen': '', 'dat': '', 'acc': '', 'abl': ''}
this_these_Masculine_Plural = {'nom': '', 'gen': '', 'dat': '', 'acc': '', 'abl': ''}

# hic, this/these feminine
this_these_Feminine_Singular = {'nom': '', 'gen': '', 'dat': '', 'acc': '', 'abl': ''}
this_these_Feminine_Plural = {'nom': '', 'gen': '', 'dat': '', 'acc': '', 'abl': ''}

# hic, this/these neuter
this_these_Neuter_Singular = {'nom': '', 'gen': '', 'dat': '', 'acc': '', 'abl': ''}
this_these_Neuter_Plural = {'nom': '', 'gen': '', 'dat': '', 'acc': '', 'abl': ''}

# iste, that (near you/of yours) masculine 
that_near_you_Masculine_Singular = {'nom': '', 'gen': '', 'dat': '', 'acc': '', 'abl': ''}
that_near_you_Masculine_Plural = {'nom': '', 'gen': '', 'dat': '', 'acc': '', 'abl': ''}

# iste, that (near you/of yours) feminine
that_near_you_Feminine_Singular = {'nom': '', 'gen': '', 'dat': '', 'acc': '', 'abl': ''}
that_near_you_Feminine_Plural = {'nom': '', 'gen': '', 'dat': '', 'acc': '', 'abl': ''}

# iste, that (near you/of yours) neuter
that_near_you_Neuter_Singular = {'nom': '', 'gen': '', 'dat': '', 'acc': '', 'abl': ''}
that_near_you_Neuter_Plural = {'nom': '', 'gen': '', 'dat': '', 'acc': '', 'abl': ''}


guess = ''
score = 0
tries = 0

while guess != 'exit':

	def chooseDemonstrative():
		# N = random.randint(1,18)
		N = 1 # This line will be removed so that it will pick any one of the dictionaries to be filled in above. This is just to get it working.
		Demonstrative = {} 
		Clue1 = ''
		if N == 1:
			Demonstrative = that_those_Masculine_Singular
			Clue1 = 'That/Those, Masculine Singular Demonstrative Pronoun/Adjective'
		elif N == 2:
			Demonstrative = 'that_those_Masculine_Plural'
		elif N == 3:
			Demonstrative = 'that_those_Feminine_Singular'
		elif N == 4:
			Demonstrative = 'that_those_Feminine_Plural'
		elif N == 5: 
			Demonstrative = 'that_those_Neuter_Singular'
		elif N == 6:
			Demonstrative = 'that_those_Neuter_Plural'
		elif N == 7:
			Demonstrative = 'this_these_Masculine_Singular'
		elif N == 8:
			Demonstrative = 'this_these_Masculine_Plural'
		elif N == 9:
			Demonstrative = 'this_these_Feminine_Singular'
		elif N == 10:
			Demonstrative = 'this_these_Feminine_Plural'
		elif N == 11:
			Demonstrative = 'this_these_Neuter_Singular'
		elif N == 12:
			Demonstrative = 'this_these_Neuter_Plural'
		elif N == 13:
			Demonstrative = 'that_near_you_Masculine_Singular'
		elif N == 14:
			Demonstrative = 'that_near_you_Masculine_Plural'
		elif N == 15:
			Demonstrative = 'that_near_you_Feminine_Singular'
		elif N == 16:
			Demonstrative = 'that_near_you_Feminine_Plural'
		elif N == 17:
			Demonstrative = 'that_near_you_Neuter_Singular'
		else: 
			Demonstrative = 'that_near_you_Neuter_Plural '
		return Demonstrative, Clue1

	def chooseCase():
		c = ''
		Clue2 = ''
		number = random.randint(1,5)
		if number == 1:
			c = 'nom'
			Clue2 = 'Nominative'
		elif number == 2:
			c = 'gen'
			Clue2 = 'Genitive'
		elif number == 3:
			c = 'dat'
			Clue2 = 'Dative'
		elif number == 4:
			c = 'acc'
			Clue2 = 'Accusative'
		else:
			c = 'abl'
			Clue2 = 'Ablative'
		return c, Clue2
	
	Dem_and_clue = chooseDemonstrative()
	Case_and_clue = chooseCase()
	Dem = Dem_and_clue[0]
	Clue = Dem_and_clue[1] + ' ' + Case_and_clue[1]
	c = Case_and_clue[0]
	answer = Dem[c]

	os.system('clear')

	print(Clue)
	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