【发布时间】:2019-01-19 18:09:12
【问题描述】:
我一直在开发一个简单的测验程序,其中包含一个 2d 列表来存储问题和答案,并且在我的代码中遇到了错误“builtins.TypeError:列表索引必须是整数或切片,而不是元组”。
这是我的代码:
import random
score = 0
incorrect_answers = 0
Questions_List = [["ANAGRAM 1: 'Agnamar'. Please enter your answer: ",
"anagram"], ["ANAGRAM 2: 'Coterump'. Please enter your answer: ",
"computer"], ["ANAGRAM 3: Certcor. Please enter your answer: ", "correct"],
["ANAGRAM 4: Bleat. Please enter your answer: ", "table"]]
random.shuffle(Questions_List)
def answer (Questions_List, score):
answer = input(Questions_List[score, 0]).lower()
return answer
while score > 10:
answer = input(Questions_List, score)
if answer == Questions_List[score, 1]:
print ("Correct!")
score = score + 1
elif answer != Questions_List[score, 1]:
print ("Incorrect!")
incorrect_answers = incorrect_answers + 1
if answer == Questions_List[score, 1]:
线路出现此错误,是什么问题?
【问题讨论】:
-
只写
Questions_List[score][1]。
标签: python arrays list arraylist multidimensional-array