Simple quiz in Python that reads a csv file in "answer,question" format. The program was running before I decided to try to refactor my original code by attempting to implement OOP. I referenced different sources to determine whether the csv, random, classes and loop were coded correctly, but the code does not run with this updated refactored version.
I do not see what am I doing wrong for it not run with the refactored code. In essence, what am I doing wrong? Am I putting the code in the wrong place for it not run with oop?
import csv
import random
player_name = input("What is your name? ")
print(f"Welcome, {player_name}, to the Quiz!")
play_quiz = str(input("Are you ready to play? "))
if play_quiz != "y":
exit()
class Question:
def __init__(self, prompt, answer):
self.prompt = prompt
self.answer = answer
def quiz():
score = 0
questions_right = 0
quiz_file = open(characters_file, "r")
quiz_data = quiz_file.readlines()
random.shuffle(quiz_data)
question_number = 1
for question in range(65):
x = quiz_data[question].strip()
data = x.split(",")
Question = data.prompt[1]
correct_answer = data.answer[1]
def run_test(quiz_data):
answer = input("What is your answer? ")
if answer == correct_answer:
score += 1
question_right = question_number + 1
else:
print("Incorrect.")
print(f"Correct answer should be: {CorrectAnswer}")
total_score = (score / 65) * 100
print("You answered " + str(score) + "/" + str(total_score) + " question correct.")
print("You have a score of " + str(total_score) + "%")
return input("Want to play again? (y/n): ") == "y".lower()
quiz()
quiz_file.close()
play_again = True
while play_again:
play_again = run_test()
Aucun commentaire:
Enregistrer un commentaire