[Tutor] Hanging myself on substitution
alan.gauld@bt.com
alan.gauld@bt.com
Fri, 3 Aug 2001 17:25:25 +0100
> import random
> import string
> cards=open('\windows\desktop\cards.txt','r')
> schmack=cards.readlines()
> counter=0
> while counter <10:
> total_cards=len(schmack)
move this outside the loop since you only need to do it once.
> '''here is where my pain begins...'''
> choose=random.randint(1,%i)% total_cards #<-grrr!
The % format operator only works on strings you should do:
choose=random.randint(1,total_cards) # or use random.choice maybe?
> chosen=schmack[choose]
> del schmack[choose]
# now decrement total_cards coz youve removed one.
total_cards -= 1
> print chosen
> counter=counter+1
You could use a "for counter in range(10):" to do the same job with
less work...
Alan G.