我正在嘗試創建測驗,并且questions第二元素的數組中存在語法錯誤。我試圖通過`for循環將每個對象附加到數組,但是我需要每個問題都有正確的答案。Questions 類位于不同的文件中:class Questions: def __init__(self, prompt, answer): self.prompt = prompt self.answer = answer這是主文件:from Questions import QuestionsquestionsPrompt = ["What does CPU stand for?\n(a) central procesing unit\n(b)controlled purification\n(c)computer unit", "What is an advantage of a compiler?\n(a)slow to run each time\n(b)compiling takes a long time\n(c)easy to implement", "The Operating System is a :\n(a)system software\n(b)application software\n(c)utility software"]questions = [ Questions(questionsPrompt[0], "a") Questions(questionsPrompt[1], "b") Questions(questionsPrompt[2], "a")]def runQuiz(questions): score = 0 for question in questions: answer = input(question.prompt) if answer == question.answer: score += 1 return scorerunQuiz(questions)
2 回答

開滿天機
TA貢獻1786條經驗 獲得超13個贊
正如 Aran-Fey 評論的那樣,列表項必須用逗號分隔。對于字典、集合等其他集合也是如此。
questions = [
Questions(questionsPrompt[0], "a"),
Questions(questionsPrompt[1], "b"),
Questions(questionsPrompt[2], "a")
]

鴻蒙傳說
TA貢獻1865條經驗 獲得超7個贊
正如 Aran-Fey 指出的那樣,您的語法不正確。
questions = [
Questions(questionsPrompt[0], "a"),
Questions(questionsPrompt[1], "b"),
Questions(questionsPrompt[2], "a")
]
另外,還有一點,您正在創建的是一個列表,而不是一個數組。語義和實現都存在差異,而且由于 Python 兩者都有,所以這是一個重要的區別。
添加回答
舉報
0/150
提交
取消