# [Tutor] my first project: a multiplication trainer

Chris Fuller cfuller084 at thinkingplanet.net
Sun Mar 16 16:34:31 CET 2008

```On Sunday 16 March 2008 08:03, Guba wrote:
> Hello!
>
> I like the idea of retaining my original questions by creating a proxy
> list, but I wasn't able to understand (find) the proxy list:
>
> Chris Fuller wrote:
> > from random import choice
> >
> > questions = [ [i,j] for i in range(1,10) for j in range(1,10) ]
> >
> > choices = range(len(questions))
>
> This I don't understand: len(questions) simply gives 81;
> range(len(questions)) counts them all up: 0,1,2...80.

Each element in the proxy list is the index of a distinct element in the
original list.

>
> > while choices:
> >    proxyq = choice(choices)
>
> here choice(choices) picks ONE item  out of choices (e.g. 56) and
> assigns it to proxyq
>
> >    del choices[choices.index(proxyq)]
>
> here the item (56) just assigned to proxyq gets removed from choices
> (question: why not use pop() for these last two steps?)

choice() returns a random element from the list of choices, not its index.
One could call pop() instead of del, but del is probably a little faster, and
doesn't return a value that we wouldn't use anyway.  pop() wouldn't give us a
random element, unless passed a random argument, such as
pop(choice(range(len(choices)))), but that would be very cumbersome.

>
> >    q = questions[proxyq]
>
> here q is assigned to item 56, i.e. [7, 3], out of questions (which
> contains all 81 possible questions).
>
> Now, because we are operating in a while loop, 81 item are generated and
>  81 items are accordingly picked out of the questions list, all this
> without repetition of items..
>
> If I am right (am I?) with my interpretation, then I still don't
> understand how/where we generated a proxy list... I think we are just
> running a while loop without having generated a list out of its ouput!??

This isn't like a for loop that iterates through the elements of a list.  It
is a while loop that repeats until some condition is false.  I used a bit of
a shortcut when I used "while choices:": this is equivalent to "while
len(choices)>0", or "stop the loop when the choices list is empty".  The
deletion is necessary, so that choice() doesn't return the same element again
later.

It seems to me that a better way to do this would be to use random.shuffle()
on the choices list, and iterate through that in a for loop.
>
> Cheers for a short comment!

Ha!  How about a long one?  I have attached some example code.  It is easier
to see how this works with working code.  The first uses choice(), the
second, shuffle().
>
> Guba
-------------- next part --------------
A non-text attachment was scrubbed...
Name: mt1.py
Type: application/x-python
Size: 396 bytes
Desc: not available
Url : http://mail.python.org/pipermail/tutor/attachments/20080316/7963b925/attachment.bin
-------------- next part --------------
A non-text attachment was scrubbed...
Name: mt2.py
Type: application/x-python
Size: 357 bytes
Desc: not available
Url : http://mail.python.org/pipermail/tutor/attachments/20080316/7963b925/attachment-0001.bin
```