Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

DB2 Universal Database V7.1 Application Development Certification Guide

Authors: Steve Sanyal, David Martineau, Kevin Gashyna, Michael Kyprianou

1st Edition

0130913677, 978-0130913678

More Books

Students also viewed these Databases questions

Question

=+ c. How would the change you describe in part

Answered: 1 week ago