[Tutor] help with random.randint

Eike Welk eike.welk at gmx.net
Wed Feb 3 20:34:34 CET 2010


On Wednesday February 3 2010 12:26:43 David wrote:
> thanks for the explanation, all this is really helpful -- I certainly
> have learned sth. today!
> I wonder, though, how I would get my number pairs, which I need later
> on, if I were to follow your solution. I am asking because as I
> understand your code, the list terms is a list of integers here, but not
> of x,y pairs, right? (I can see that this was a problem of my code right
> from the start, btw.)

Maybe I don't understand you correctly; but if you know that you only need two 
random numbers I would create the list like this:

import random
xy = [random.randint(1, 99), random.randint(1, 99)]



Or maybe a use tuple and a little code beautification:

import random
randint = random.randint

xy = (randint(1, 99), randint(1, 99))



Off course you can put these small lists or tuples into another list:

import random
randint = random.randint

num_terms = 5
terms =  []
for i in range(num_terms):
     terms += [(randint(1, 99), randint(1, 99))]

print terms



Eike.


More information about the Tutor mailing list