Help with random and csv

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

You’re picking a random number, but not getting the relevant row from rows. row is still set to the last row from when you read them in.

Incidentally, indexes start at 0 in Python, and csvreader.line_num is the number of lines read, which might not be what you want; the number of questions, i.e. len(rows), would be better. Also, you’re picking 3 random numbers, but not checking for repetition. Try shuffling the rows instead (the random module has a function for that).

You can pick random item from list directly and it feels like csv.DictReader could be used:

from csv import DictReader
from random import choice


with open('quiz_questions.csv', 'r') as f:
    keys = next(f).strip().split(',')
    data = list(DictReader(f, fieldnames=keys))

question = choice(data)

data will look like below. With choice it is possible to randomly pick a dictionary from the list. Dictionary keys should be better for readability compared to indices. It easy to make desired output to screen using keys (question['Question']).

[{'Question': 'What Colour is the sky', 'A': 'Red', 'B': 'Purple', 'C': 'Blue', 'Correct': 'C', 'prize': '1000'},
 {'Question': 'What colour is grass', 'A': 'Green', 'B': 'Red', 'C': 'Black', 'Correct': 'A', 'prize': '250'},
 {'Question': 'What year did the Titanic sink in the Atlantic Ocean on 15 April, on its maiden voyage from Southampton', 'A': '1915', 'B': '1912', 'C': '1908', 'Correct': 'B', 'prize': '1500'},
 {'Question': 'What is the title of the first ever Carry On film made and released in 1958', 'A': 'Carry on Dear', 'B': 'Carry on Seargent', 'C': 'Carry on Hitler', 'Correct': 'B', 'prize': '500'},
 {'Question': 'What is the name of the biggest technology company in South Korea', 'A': 'Sony', 'B': 'Apple', 'C': 'Samsung', 'Correct': 'C', 'prize': '250'}]