[Tutor] Generating random in a user specified range.

Nick Lunt nick at javacat.f2s.com
Tue Apr 27 12:20:52 EDT 2004


Hi Adam,

im not sure of line 'range = range()' .
That errors when I try it here at home.

Are you aware that range() is a built in keyword ?

Regards
Nick.


-----Original Message-----
From: tutor-bounces at python.org [mailto:tutor-bounces at python.org]On
Behalf Of Adam
Sent: 27 April 2004 14:22
To: tutor at python.org
Subject: [Tutor] Generating random in a user specified range. 


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 
complains.

Any advice on what I can do to fix this?
Thanks.

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 
(bottom,top)
     print "Target is ", target
     ok = raw_input("please press enter to continue")
     return target


_______________________________________________
Tutor maillist  -  Tutor at python.org
http://mail.python.org/mailman/listinfo/tutor



More information about the Tutor mailing list