Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

TypeError: 'list' object is not callable Python Object Oriented Programming

I am trying to create a question bank with questions and answers(True or False). I have a file called data.py with the question data:

question_data = [
    {"text": "A slug's blood is green.", "answer": "True"},
    {"text": "The loudest animal is the African Elephant.", "answer": "False"},
    {"text": "Approximately one quarter of human bones are in the feet.", "answer": "True"},
    {"text": "The total surface area of a human lungs is the size of a football pitch.", "answer": "True"},
    {"text": "In West Virginia, USA, if you accidentally hit an animal with your car, you are free to take it home to eat.",
     "answer": "True"},
    {"text": "In London, UK, if you happen to die in the House of Parliament, you are entitled to a state funeral.",
     "answer": "False"},
    {"text": "It is illegal to pee in the Ocean in Portugal.", "answer": "True"},
    {"text": "You can lead a cow down stairs but not up stairs.", "answer": "False"},
    {"text": "Google was originally called 'Backrub'.", "answer": "True"},
    {"text": "Buzz Aldrin's mother's maiden name was 'Moon'.", "answer": "True"},
    {"text": "No piece of square dry paper can be folded in half more than 7 times.", "answer": "False"},
    {"text": "A few ounces of chocolate can to kill a small dog.", "answer": "True"}
]

I also have a Question class:

class Question:
    def __init__(self, text, answer):
        self.question_text = text
        self.question_answer = answer

This is my code:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

from question_model import Question
from data import question_data

question_bank = []

for question in question_data():
    q_text = question["text"]
    q_answer = question["answer"]
    new_q = Question(q_text, q_answer)
    print(new_q)
    question_bank.append(new_q)
    print(question_bank)

Whenever I run my code, it gives me the error:

for question in question_data():
TypeError: 'list' object is not callable

What should I fix?

>Solution :

  1. question_data is a list.
  2. question_data() calls this list.
  3. It’s impossible to call a list (what would it mean to call a list anyway?), so you get the error.

Simply don’t call the list:

for question in question_data:
    # do stuff
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading