[EMAIL PROTECTED] wrote:
> I am new to Python (and programming). In teaching myself I wrote a
> small program that will pick random lottery numbers, I want to check for
> duplicates and rerun the random function. But I am hitting a wall.
> This is what I think should work, but I still get duplicates. TIA.
>
>
>
> print "\n"
> count = input("How many quick picks do you need? ")
> print "\n"
> num = 0
> while count > 0:
> num1 = randint(1,55)
> num2 = randint(1,55)
> while num2 == num1:
> # looking for duplicates
> num2 = randint(1,55)
> num3 = randint(1,55)
> while num3 == (num1 or num2):
This doesn't do what you want; (num1 or num2) is evaluated first; the
value of this expression will be num1, since num1 is not 0. Then you
compare num3 to the result.
You need
while num3 == num1 or num3 == num2:
or, a version that scales better:
while num3 in [ num1, num2 ]
or even better, take a look at random.sample()
Kent
> # looking for duplicates
> num3 = randint(1,55)
> num4 = randint(1,55)
> while num4 == (num1 or num2 or num3):
> # looking for duplicates
> num4 = randint(1,55)
> num5 = randint(1,55)
> while num5 == (num1 or num2 or num3 or num4):
> # looking for duplicates
> num5 = randint(1,55)
> pb = randint(1,42)
> count = count - 1
> answer = [num1, num2, num3, num4, num5]
> answer.sort()
> num = num + 1
> print "#",num, answer, "and for the powerball:", pb
> print "\n"
>
> _______________________________________________
> Tutor maillist - [email protected]
> http://mail.python.org/mailman/listinfo/tutor
>
>
_______________________________________________
Tutor maillist - [email protected]
http://mail.python.org/mailman/listinfo/tutor