Python Forum
Thread Rating:
  • 2 Vote(s) - 3.5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Quiz using a csv
#1
I am currently in the process of making a quiz in Python, all the questions are stored in a csv database. I am having difficulty in having multiple answers to one question in the quiz. Note: I do not have the code on this computer so I quickly made something up along the lines of the real thing, the version below does not include everything:

file=open("easydatabase.csv","r")
answer = input("\n" + detail[0])
    for line in file:
        details=line.split(",")
        if details[1] == answer:
        total = total + 1
            print("Correct! Added 1 to your score.")
The main issue I am having is having the correct answer to be equal to multiple cells in the csv file, thanks in advanced for the help! (I was trying different things such as writing 'details[1,2]' but I had no luck)
Reply
#2
What is the form of the CSV?

I'm assuming something like,
"question1", "correct1", "alt1 1", "alt1 2", "alt 3"
"question2", "correct2", "alt2 1", "alt2 2", "alt2 3"
...
"questionn", "correctn", "altn 1", "altn 2", "altn 3"
You present each question in turn and the answers in random order. (Labeled A, B, C or whatever.) Then check if the selected option matches the correct answer.

I appreciate you don't have the proper code you developed with you, but what you've shared has a few too many gaps in it to make much sense. What variable is detail for example?
I am trying to help you, really, even if it doesn't always seem that way
Reply
#3
I read this as if all the answers for a given question are correct. Like on Jeopardy, where they'll accept an alternate answer. If that's the case, store all of the answers in a list, and check to see if the user's answer is in the list (the 'in' operator). That works whether you have one answer or multiple answers.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020