I'm writing a program to choose a random line from a text file using a while loop as a counter and then another variable to choose the random line:
import random
fortunes_file = open("fortunes.txt", "r")
fortunes = fortunes_file.readline()
count = 0
while fortunes != "":
count += 1
fortunes = fortunes_file.readline()
rand_line = random.randint(1, count)
print fortunes[rand_line]
fortunes_file.close()
However, I get the following error when trying to run the program:
IndexError: string index out of range
Aucun commentaire:
Enregistrer un commentaire