[Tutor] Problem with while loop

Michael Connors connorsml at gmail.com
Mon Sep 10 13:28:47 CEST 2007


Hi,
I would do it as follows, adding 0s to front to make them valid PINs.

while counter < howmany:

    pin = randint(0000,9999)
    print "%04i" % (pin)
    counter += 1

On 10/09/2007, Vishnu Mohan <vishnu at montalvosystems.com> wrote:
>
>
> > Now I just need to figure out how to only get 4 digit pin numbers :)
> >
> Use regular expressions....
> The following is the code with re.
>
>
> from random import randint
> import re
>
> counter = 0
> pinPattern = re.compile(r'^\d{4}$')
> howmany = raw_input( "How many: " )
> if pinPattern.match(howmany):
>     while counter < int(howmany):
>         pin = randint(0000,9999)
>         print pin
>         counter += 1
> else:
>     print "%s is not valid 4 digit integer"%howmany
>
>
> -vishnuMohan
>



-- 
Michael Connors
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20070910/d71b35db/attachment.htm 


More information about the Tutor mailing list