社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  Python

TypeError:“list”对象不可调用Python面向对象编程[closed]

WolfGirl • 3 年前 • 1442 次点击  

我正在尝试创建一个包含问题和答案(对或错)的题库。我有一个叫做数据的文件。请回答以下问题:

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"}
]

我还有一个问题课:

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

这是我的代码:

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)

每当我运行代码时,它都会给出错误:

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

我该修什么?

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/130146
 
1442 次点击  
文章 [ 2 ]  |  最新文章 3 年前
Mark Cole
Reply   •   1 楼
Mark Cole    3 年前

我遇到过几次常见错误-问题_数据是一个列表,不可调用。只需从for循环中删除括号。

ForceBru
Reply   •   2 楼
ForceBru    3 年前
  1. question_data 这是一份清单。
  2. question_data() 调用此列表。
  3. 不可能调用列表(调用列表到底意味着什么?),所以你得到了错误。

简单地说,不要给列表打电话:

for question in question_data:
    # do stuff