首页 > 解决方案 > 制作问答游戏(Python)

问题描述

我是 Python 编程的初学者,刚刚完成了我在 python 中的程序级编程。

我正在使用面向对象编程制作一个问答游戏。我遇到了一个我无法解决的问题。

这是我的代码:

from question_model import Question
from data import question_data

question_bank = []

for i in question_data:
    for j in range(0, 13):
        questions_ans = Question(i[j].key, i[j].value)
        question_bank.append(questions_ans)

print(question_bank)

question_model文件是:

class Question:

def __init__(self, text, answer):
    self.text = text
    self.answer = answer

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

这是我的代码图像:

在此处输入图像描述

如控制台中所示,该question_bank列表也是空的。它应该给出位置地址,不是吗?

而且,question_data是文件中的一个变量,data由字典列表组成。我正在尝试遍历字典列表,所以我使用了 2 个for-loops

该代码没有显示任何错误,否则它可以给我一些关于我错在哪里的提示。我的迭代和追加方式是否正确?

你能指出上面代码中的错误吗?

标签: pythonclass

解决方案


这是您如何将 Question 类的实例添加到您的 question_bank 列表的方法:

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

question_bank = []
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"}
]

for item in question_data:
    question_bank.append(Question(item["text"], item["answer"]))

print(question_bank)

但是,如果您想从这些对象中获取问题和答案,您必须使用这些类的属性:

for item in question_bank:
    print(item.text)
    print(item.answer)

输出

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

推荐阅读