[Tutor] A row of cards, but they're all red
Danny Yoo
dyoo at hkn.eecs.berkeley.edu
Mon Jan 30 08:18:16 CET 2006
> Could someone help me understand the output I'm getting from the snippet
> below? I thought I would get a nice row of cards, 13 reds, 13 blues,
> etc. But what I get is a row of all red cards. The rank is correct on
> the cards, but they are all red.
Hi Terry,
There is an issue with the loop here:
> for suit in suits:
> for x, rank in enumerate(ranks):
> cards.append([suit, rank, value[x], images[x]])
Let's assume for the moment that everything else is ok, and that images is
a list of fifty-two elements. The loop above tries to associate each
individual image with a unique card.
Think about what values 'x' goes over in the loop, and you should see a
problem. Again, try matching up the experience you're seeing --- all red
cards --- and try doing a "what if?" on the situation here.
Side note: you might find a funky operator called the "modulo" operator
very useful. Here's what it looks like:
######
>>> for hour in range(24):
... print hour, hour % 12
...
0 0
1 1
2 2
3 3
4 4
5 5
6 6
7 7
8 8
9 9
10 10
11 11
12 0
13 1
14 2
15 3
16 4
17 5
18 6
19 7
20 8
21 9
22 10
23 11
######
That is, "modulo" makes numbers "wrap" around. More formally,
m % n
ends up being the remainder if we divide m by n. Believe it or not, this
can be very handy at times. (Otherwise, why would I mention it here?
*grin*) We can talk about this in more detail if you'd like.
Good luck to you!
More information about the Tutor
mailing list