亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

創建對象列表python

創建對象列表python

qq_遁去的一_1 2021-05-30 14:02:25
我正在嘗試創建測驗,并且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")

]


查看完整回答
反對 回復 2021-06-01
?
鴻蒙傳說

TA貢獻1865條經驗 獲得超7個贊

正如 Aran-Fey 指出的那樣,您的語法不正確。


questions = [

    Questions(questionsPrompt[0], "a"),

    Questions(questionsPrompt[1], "b"),

    Questions(questionsPrompt[2], "a")

]

另外,還有一點,您正在創建的是一個列表,而不是一個數組。語義和實現都存在差異,而且由于 Python 兩者都有,所以這是一個重要的區別。


查看完整回答
反對 回復 2021-06-01
  • 2 回答
  • 0 關注
  • 166 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號