[Tutor] Problem with while loop

Ricardo Aráoz ricaraoz at gmail.com
Sat Sep 8 02:04:50 CEST 2007


matte wrote:
> Hi guys
> 
> Please excuse me but I've been out of Python for a while since my laptop
> was stolen...
> 
> I'm battling with a very basic while loop....
> 
> -------------- 8< ----------------------
> #!/usr/bin/python
> 
> from random import randint
> 
> counter = 0
> 
> howmany = raw_input( "How many: " )
> 
> while counter < howmany:
> 
>     pin = randint(0000,9999)
>     print pin
>     counter += 1
> 
> -------------- 8< ----------------------
> 
> For some reason if I use an integer in place of "howmany" it works, but I'd
> like it to work as I can logically see it above.
> 
> What am I missing ?
> 

raw_input returns a string. Have to convert it.

BTW, instead of the while loop you might try this :

for i in xrange(int(howmany)) :
	print randint(0000, 9999)

saves you some code and a few variables.
Or if what you want is a list for further processing :

randomInts = [randint(0000, 9999) for i in xrange(int(howmany))]









More information about the Tutor mailing list