mercredi 19 octobre 2016

Print random line from txt file?

I'm using random.randint to generate a random number, and then assigning that number to a variable. Then I want to print the line with the number I assigned to the variable, but I keep getting list index out of range as an error. Here's what I tried:

f = open(filename. txt)
lines = f.readlines() 

rand_line = random. randint(1,10)
print lines[rand_line]




Aucun commentaire:

Enregistrer un commentaire