Re: [Tutor] Storing dictionary value, indexed by key, into a variable
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 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!
Re: [Tutor] Storing dictionary value, indexed by key, into a variable
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 = '
[Tutor] Storing dictionary value, indexed by key, into a variable
Hey all, I am writing a program to drill the user on Latin demonstrative pronouns and adjectives (DPA). It displays a description, and the user has to enter the DPA that corresponds to the description. DPA vary for gender, number and case, and there are 3 separate DPA. I have these stored in a bunch of dictionaries, with the DPA, gender and number in the dictionary name and the cases as keys. Of course, the values are the DPA themselves. Like so: that_those_Masculine_Singular = {'nom': 'ille', 'gen': 'illīus', 'dat': 'illī', 'acc': 'illum', 'abl': 'illō'} I have a function that randomly selects one of these dictionaries, and another that randomly selects strings corresponding to the keys ('nom', 'gen', etc.). The trouble begins somewhere along here: D = chooseDict() c = chooseCase() print(D, c) guess = '' # code to get the guess # then, answer = D[c] if guess == answer: # Do stuff, change score, continue, etc. This doesn't work, and I get this error: TypeError: string indices must be integers So my question is, why does Python think that D is a string? When I type the actual names (i.e., that_those_Masculine_Singular["nom"]) the answer is returned just fine. I have tried D['c'] and D["c"] also, and got the same error. I searched the web, and I can find no explanation on how to do what I am doing, and I can find nothing that indicates why this doesn't work. I'd really appreciate any help! Thank you, J ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] basic function concept
Too bad that doesn't work. On Nov 16, 2013, at 11:16 PM, Alex Kleider wrote: > On 2013-11-16 13:20, Byron Ruffin wrote: >> def main(x, y, z): >> print (x, y, z) >> def funct(): >> x = 1 >> y = 2 >> z = 3 >> return x, y, z >> main() >> Can someone tell me why main is not being given any arguments? > > Because you didn't give it any. > Try > main(funct()) > instead. > > >> ___ >> Tutor maillist - Tutor@python.org >> To unsubscribe or change subscription options: >> https://mail.python.org/mailman/listinfo/tutor > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor