I have my classes and data in different files and I hold my data as a dictionary.
class QuizBrain:
def __init__(self, question_list):
self.question_number = 0
self.question_list = question_list
self.score = 0
def still_has_questions(self):
return self.question_number < len(self.question_list)
def next_question(self):
current_question = self.question_list[self.question_number]
self.question_number += 1
user_answer = input(f"{self.question_number}: {current_question} (True/False): ")
def check_answer(self, user_answer, correct_answer):
if user_answer.lower() == correct_answer.lower():
self.score += 1
print("You got it right!")
else:
print("That's wrong")
print(f"The correct answer was: {correct_answer}")
print(f"Your current score is {self.score}/{self.question_number}")
print("\n")
class Question:
def __init__(self, text, answer):
self.text = text
self.answer = answer
I'm watching a code tutorial, and they have no problem running this code. When I run the code, I get the ram addresses:
1: <question_model.Question object at 0x7b7af8a0bb60> (True/False): True
2: <question_model.Question object at 0x7b7af8a0bef0> (True/False): False
3: <question_model.Question object at 0x7b7af8a0bf50> (True/False): True
4: <question_model.Question object at 0x7b7af8a0bf80> (True/False): True
5: <question_model.Question object at 0x7b7af8a0bfb0> (True/False):
Sorry if this is a dumb question, I'm not very experienced. This is my main.py
from data import question_data
from question_model import Question
from quiz_brain import QuizBrain
question_bank = []
for question in question_data:
question_text = question["text"]
question_answer = question["answer"]
new_question = Question(question_text, question_answer)
question_bank.append(new_question)
quiz = QuizBrain(question_bank)
while quiz.still_has_questions():
quiz.next_question()
print("You've completed the quiz!")
print(f"Your final score was: {quiz.score}/{quiz.question_number}")
You are printing the object, instead of the question field.
In your QuizBrain
class, update next_question
method to print current_question.text
instead of current_question
:
def next_question(self):
current_question = self.question_list[self.question_number]
self.question_number += 1
user_answer = input(f"{self.question_number}: {current_question.text} (True/False): ") # Update this line
Another option would be to add __str__
method to the Question
class:
def __str__(self):
return self.text
With this option, anytime you try to print a Question
object, it will automatically call __str__
and output Question.text
instead of what you are seeing now.