EXTREME NOOB, lists?
Alex Martelli
aleaxit at yahoo.com
Sat Sep 2 04:53:36 EDT 2000
"skeetor" <skeetornospam at bellsouth.net> wrote in message
news:%FZr5.6738$yl2.92225 at news4.atl...
> Ok, apparently I was unclear. This language is still unfamiliar to me...
> please help me.
Sure.
> if a == 4:
> dice_numbers (a)
> elif a == 6:
> dice_numbers (a)
> elif a == 8:
> dice_numbers (a)
> elif a == 10:
> dice_numbers (a)
> elif a == 12:
> dice_numbers (a)
> elif a == 20:
> dice_numbers (a)
> elif a == 100:
> dice_numbers (a)
> else:
> print ("try again")
Change this sequence to:
if a in (4,6,8,10,12,20,100):
dice_numbers(a)
else:
print("try again")
The key idea is the statement form:
if needle in haystack:
which is satisfied if, and only if, the object referred to by 'needle'
is one of the items in the sequence referred to by 'haystack'. This
gives a compact and elegant solution to a frequent need.
There are other ways, but this one is by far the neatest, and the
one I would strongly advise at this point of your Python learning.
Alex
More information about the Python-list
mailing list