[英]Making a Quiz Game (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 。
該代碼沒有顯示任何錯誤,否則它可以給我一些關於我錯在哪里的提示。 我的迭代和追加方式是否正確?
你能指出上面代碼中的錯誤嗎?
這是您如何將 Question class 的實例添加到您的 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)
但是,如果您想從這些對象中獲得問題和答案,您必須使用這些 class 的屬性:
for item in question_bank:
print(item.text)
print(item.answer)
Output
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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.