[Tutor] Re: ...and another thing

Charlie Clark Charlie Clark <charlie@begeistert.org>
Fri, 03 Aug 2001 18:14:42 +0200


>import random
>file_in = open('\windows\desktop\cards.txt','r')
>cards = file_in.readlines()
>
>
>for i in range(3):    #I think your first example would have been
>                      #range(num_cards) to deal all the cards
>    card = random.randint(0, num_cards)
>    print cards[card]
>    del demo[card]
oops need to correct myself here. You're right, as num_cards changes each 
loop it should be inside the loop itself. Having said that it should just be 
a reference... oh well. it needs to be in the loop. And, of course, I made 
the same mistake as you about forgetting the difference betweent the number 
of items in an object and indexing them.

The correct code should be:
for i in range(3):   # or range(whatever)
    num_cards = len(cards)
    card = random.randint(0, num_cards-1)
    print cards[card]
    del demo[card]

Charlie