Question: I have a simple quiz in Python that's having issues. It reads a comma-separated text file in question,answer format. The program is reading the quiz
I have a simple quiz in Python that's having issues. It reads a comma-separated text file in "question,answer" format. The program is reading the quiz fine and outputs the questions, but I can't get the questions to randomize and the "CorrectAnswer" variable to read the correct answer.
For instance, if the question is, "What is the capital of California?" and the answer is "Sacramento", the CSV file has it written like this: "What is the capital of California,Sacramento". But the program can't reference the answer, only the first part before the comma.
What am I doing wrong?
def quiz(): score=0 questionsRight=0 fileName = input("Please enter the name of the quiz file: ") quizFile = open(fileName,"r") quizData = quizFile.readlines() questionno=1 for x in range(10): for x in quizData: data = x.split(",") random.shuffle(quizData) questions = data[0] CorrectAnswer = data[1]
print(data) print("Question #",questionno) print(questions) answer = input("What is your answer? ") if answer == CorrectAnswer: print("Correct!") score=score+1 questionsRight=questionsRight+1 questionno = questionno+1 else: print("Incorrect.") questionno = questionno+1
totalScore = (score / 10) * 100 print("You got ",score," questions right, and a score of ",totalScore,"%.")
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
