John Aten wrote:

> 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?

Instead of the many if...elif switches try to put the alternatives into a 
list, e. g.

>>> cases = [
... "Nominative",
... "Genitive",
... "Dative",
... "Accusative",
... "Ablative"
... ]

(These could also be tuples cases = [("nom", "Nominative"), ...] but I 
wouldn't bother because you can derive "nom" from "Nominative" when the need 
arises. How?)

You can then pick a random number in the range
0 <= random_number < len(cases) 
to choose an item from the list:

>>> import random
>>> cases[random.randrange(len(cases))]
'Accusative'
>>> cases[random.randrange(len(cases))]
'Ablative'

This operation is so common that there is a dedicated function:

>>> random.choice(cases)
'Dative'

Reimplement the chooseCase() function with this approach first and then see 
if you can build an appropriate list for an analogous implementation of 
chooseDemonstrative(). Come back here for more hints if you are stuck.

_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor

Reply via email to