[Tutor] Generating random in a user specified range.

Adam adam at monkeez.org
Tue Apr 27 09:21:57 EDT 2004

I'm trying to develop an app where the user can specify the range in 
which a random number will be generated from.

So far, I have two functions; range collects the range in which the 
random number is to be generated from and generate number is the one 
which creates the number. Range produces a list, which holds the top and 
bottom number which is returned and then is passed to generatenumber to 
create a number.

However, I seem to have a problem - I get an error using 
random.randint(a,b) and with random.randrange(a,b). It seems that it 
might be fussy accepting parts of a list as the argument. I've tried 
converting them to integers and then passing the interger, but it still 

Any advice on what I can do to fix this?

The driving code is:

range = range()
print range
raw_input("click ok to carry ok")
target = generatenumber(range) #kicks the whole thing off

def range():
	strtop = raw_input("What is your top number?")
	strbottom = raw_input("What is your bottom number?")
	top = int(strtop)
	bottom = int(strbottom)
	range = [bottom, top]
	print "range top is ", range[0]
	print "range bottom is ", range[1]
	return range

def generatenumber(range):
     top = int (range[0])
     bottom = int (range[1])
     target = random.randrange(range[1], range[0]) # and I've also tried 
     print "Target is ", target
     ok = raw_input("please press enter to continue")
     return target

More information about the Tutor mailing list