[Tutor] Empty Range for RandRange

Kent Johnson kent37 at tds.net
Wed Apr 18 03:09:28 CEST 2007


Ellen Kidda wrote:
> Would someone kindly review the following code and error?  This sample 
> program is copied from Python Programming for the Absolute Beginner (2nd 
> ed.)  I'm running Python 2.3.5 (#62, Feb  8 2005, 16:23:02) [MSC v.1200 
> 32 bit (Intel)] on win32.  (Python 2.3 is the version referenced in this 
> edition.)  Referenced lines 8 and 9 of the error are the 3rd and 2nd 
> lines from the last in the code.  Thank you very much.
> Ellen
> 
> #Demonstrates string indexing
> 
> import  random
> 
> word = "index"
> 
> print "The word is: ", word, "\n"
> 
> high = len(word) 
> 
> low = len(word)  

Notice that low and high are the same.
> 
> for i in range(10):
> 
>     position = randrange(low, high)

This picks from the range low <= number < high. Since low == high the 
range is empty.

Kent

> 
>     print "word[", position, "]\t", word[position]
> 
> raw_input("\n\nPress the enter key to exit.")
> 
> 
> 
> line 8, in -toplevel-position = random.randrange(low, high)  #line 9 
> error:  "empty range for randrange"
>   File "C:\Python23\lib\random.py", line 178, in randrange
>     raise ValueError, "empty range for randrange()"
> ValueError: empty range for randrange()
> 
> 
> 
> ------------------------------------------------------------------------
> 
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor



More information about the Tutor mailing list