Python Forum
Help with Python Inheritance (One parent, two children)
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help with Python Inheritance (One parent, two children)
#1
class Question:
    def __init__(self, prompt, answer):  
        self.prompt = prompt            
        self.answer = answer


question_prompts = [  # array
    "What color are apples?\n(a) Red/Green\n(b) purple\n(c) Orange\n\n",
    "What color are bananas?\n(a) Teal\n(b) Magenta\n(c) Yellow\n\n",
    "What color are strawberries?\n(a) Yellow\n(b) Red\n(c) Blue\n\n"
]

questions = [
    Question(question_prompts[0], "a"),  
    Question(question_prompts[1], "c"),  
    Question(question_prompts[2], "b"),
]


def run_test(questions):
    score = 0
    for question in questions:
        answer = input(question.prompt)
        if answer == question.answer:
            score += 1
    print("You got " + str(score) + "/" + str(len(questions)) + " correct")

run_test(questions)
This is my first time using Python. I have to make question_prompts, and questions the children. I have to make Question the parent. I've experimented in many ways trying to make each a separate class, I've spent a while trying to figure it out along with reading multiple articles. I am a beginner and would appreciate any help Smile
Reply
#2
Not sure what you are asking. Currently you have a class called Question, and questions is an array of objects of type Question. Can you clarify what you are wanting to do?
Reply
#3
Okay so I'm trying to call the children functions from the parent function. So questions and run_test are the children, and I'm trying to make it in a way where they are called from the parent function (question_prompts)
Reply
#4
I am afraid you are confused big time with concepts and terminology. Best would be to revisit your course material/study notes, etc.

Inheritance, parent, child are concept and terms from object oriented programming (OOP). You have one class and no inheritance whatsoever
question_prompts and questions are lists, not functions. The only function is run_test.

Post your assignment verbatim or explain better what is required
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to print out the children index of the search tree? longmen 7 2,197 Apr-02-2022, 06:58 AM
Last Post: Yoriz
  Sorting Santa's List of Children sirox84 4 5,196 Feb-20-2017, 06:10 PM
Last Post: sirox84

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020