[Tutor] Re: ...and another thing
Charlie Clark
Charlie Clark <charlie@begeistert.org>
Fri, 03 Aug 2001 18:08:21 +0200
>import random
>import string
>cards=open('\windows\desktop\cards.txt','r')
>schmack=cards.readlines()
>counter=0
>while counter <3:
> total_cards=len(schmack)
> choose=random.randint(1,string.atoi(`total_cards`))
> chosen=schmack[choose]
> del schmack[choose]
> print chosen
> counter=counter+1
mm, I'd rewrite this a little for legibility. What d-man has already said
about the random function seems to cover most of the problem but I wonder why
you are using 'while'. I think a 'for' construction would make more sense as
it does the iterating for you so there is no need to count manually.
import random
file_in = open('\windows\desktop\cards.txt','r')
cards = file_in.readlines()
num_cards = len(cards)
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]
Charlie (hopeless at programming but interested in style)