On Jan 23, 2005, at 22:08, Liam Clarke wrote:

Don't you mean

x=random.randint(0, lenoflist) ?? I'm assuming you want an integer.

random.randrange() returns an item (which can be a float or whatever, but by default is an int) in the specified range. In that case, an int between 0 and lenoflist.
The advantage over random.randint is that you can specify a step. Thus,
random.randrange(0, 257, 2) will return an even number between 0 and 256 inclusive.


While although this code below does work many times
nothing is printed out.
import random
i = 0
while i < 10:
    file = open('test.rantxt')
    listcontents = file.readlines()
    file.close()
    lenoflist = len(listcontents)#-1
    x = random.randrange(0,lenoflist)
    print listcontents[x], i
    i = i +1

Anyway, the problem with this function is that len returns a number of items, but Python, like most good programming languages (and mathematicians), counts starting from 0. Thus, the first element in a list is foo[0].
foo[len(foo)] will raise an IndexError.


-- Max
maxnoel_fr at yahoo dot fr -- ICQ #85274019
"Look at you hacker... A pathetic creature of meat and bone, panting and sweating as you run through my corridors... How can you challenge a perfect, immortal machine?"


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

Reply via email to