David Holland wrote:
Kent,
I know that you know far more about python than me but is that right ? I created file with 4 records and this code did print them all randomly:-
import random i = 0 while i < 10: file = open('filename') listcontents = file.readlines() file.close() lenoflist = len(listcontents)-1 x = random.randrange(0,lenoflist) print listcontents[x], i i = i +1
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
It sounds like your file has a blank line at the end. What do you get if you print len(listcontents)? Is it 4 or 5? Where do you think 'nothing' is coming from?
Try this:
import random
listcontents = [1,2,3,4]
for i in range(10):
lenoflist = len(listcontents)-1
x = random.randrange(0,lenoflist)
print listcontents[x]I never get a 4...
Kent
_______________________________________________ Tutor maillist - [email protected] http://mail.python.org/mailman/listinfo/tutor
