Hi All,
so I just started playing around with python, and really am just playing around to teach myself
I have a csv with the following info
Question,A,B,C,Correct,prize
What Colour is the sky,Red,Purple,Blue,C,1000
What colour is grass,Green,Red,Black,A,250
"What year did the Titanic sink in the Atlantic Ocean on 15 April, on its maiden voyage from Southampton",1915,1912,1908,B,1500
What is the title of the first ever Carry On film made and released in 1958,Carry on Dear,Carry on Seargent,Carry on Hitler,B,500
What is the name of the biggest technology company in South Korea,Sony,Apple,Samsung,C,250
In my python script, I want to pick a random question (Column 0)
i have print lines in my code, so I know the random part is working, but I can’t get it to print the question corresponding with the correct row. it just prints the last rows question
I have my code below (Please excuse my coding, I am learning and fixing things as I go)
print("Welcome to Python Trivia")
# importing csv and random module
import csv
import random
# csv file name
filename = "questions.csv"
# initializing the titles and rows list
fields = []
rows = []
# reading csv file
with open(filename, 'r') as csvfile:
# creating a csv reader object
csvreader = csv.reader(csvfile)
# extracting field names through first row
fields = next(csvreader)
# extracting each data row one by one
for row in csvreader:
rows.append(row)
# get total number of rows
for q in range(0, 3):
print("Total no. of rows: %d"%(csvreader.line_num), "\n")
endnum = csvreader.line_num
#Get random row number
qnum = random.randrange(1, endnum)
#Print to check Random is working ----Remove
print("using question", qnum, "from a possible", endnum, "questions \n\n")
print("Playing for $", row[5])
print(row[0], "?") #row[0] should be question
print("is it\n", "A ", row[1], "B ", row[2], "C ", row[3])
answer = input("A, B or C: \n ").upper()
What have I done wrong?
Thanks for your help