On Thu, Jul 3, 2008 at 9:38 PM, Mike Meyer
#while True idiom (current Python) while True: number = random.randint(1, 100) if number not in picked_numbers: break
I don't believe that actually qualifies as "current python", any more than the other current alternative:
# duplicate setup (current Python) number = random.randint(1, 100) while number in picked_numbers: number = random.randint(1, 100)
does.
Both bits of code smell. One has the duplicated line; one has a test of a constant and a block exiting from the middle, all of which make the code slightly less clear.
Yup. Non-for loops just aren't very strong in Python.
I tend to write either one, depending on what the duplicated code looks like. For this case, I actually prefer the latter. On the other hand, I'd probably write this one using a *third* alternative idiom:
# Picked initial value (current Python) number = picked_numbers[0] while number in picked_numbers: number = random.randint(1, 100)
I didn't say that the picked_numbers list had to be non-empty so you lose. :) -- mvh Björn