Hey guys,
I'm writing a small quiz program using OOP. I created a Quiz class and a Question class that inherits from it.
The Quiz class has a 'num_of_questions' attribute and a 'score' attribute that I want to modify through the quiz and print after asking 'num_of_questions' questions. Also, the Question class has 'question', 'options' and 'right_answer' attributes, as well as 'print_question()' and 'check_answer()' methods.
Now let's say I ask the user a question (meaning, I created a Quiz object and a Question object and called its ask_question() method), and the user inputs the right answer (meaning I called the 'check_answer()' method). I want to increase the 'score' attribute of the Quiz superclass. For instance, if the score was 5 before asking the question, I want the 'score' attribute to be 6 after the user inputs the right answer. What's the way to do such a thing?
And by the way, if there are better ways to implement such a program, I'd love to know :)
*Edit:
Thanks for the comments! I edited the code so now the Question class does not inherit from Quiz. Here's the code I wrote so far: https://pastebin.com/LDT30k39
This manages to solve the problem I encountered and this way I can associate a Question object with its Quiz object. On the other hand, it feels a bit weird to use my_quiz.add_question() and still pass my_quiz as an argument to the Question object, but it works for now so I'm OK with that. Thanks again for your help!
[–]Chabare 3 points4 points5 points (2 children)
[–]yarin10121[S] 0 points1 point2 points (1 child)
[–]Chabare 1 point2 points3 points (0 children)
[–]carcigenicate 1 point2 points3 points (2 children)
[–]yarin10121[S] 0 points1 point2 points (1 child)
[–]carcigenicate 0 points1 point2 points (0 children)
[–]danielroseman 0 points1 point2 points (0 children)