I have been creating a quiz game, and have to select a random line from a file. In my main code, it isn't working, so I rewrote the code in a separate file.
#Import relevant modules
import csv
import time
import random
#declare placeholder variables
a=0
b=0
#create a random line function
def randomLine(fname):
lines = open(fname).read().splitlines()
return random.choice(lines)
def runQuiz():
a=1
c=0
print ('Welcome to the Quiz!')
while b == 0:
chosenLine = randomLine('songlist.csv')
print(chosenLine)
while chosenLine[c] != ',':
c += 1
print (c)
#run the function
runQuiz()
This is the code, and when I run it it gives me the expected outputs then this error
Traceback (most recent call last):
File "Z:/NEA2020/OneDrive_1_12-11-2020/2.py", line 29, in <module>
runQuiz()
File "Z:/NEA2020/OneDrive_1_12-11-2020/2.py", line 23, in runQuiz
while chosenLine[c] != ',':
IndexError: string index out of range
Any idea what's causing it?