【问题标题】:Python tuple error when working with 2d lists in a quiz program在测验程序中使用二维列表时出现 Python 元组错误
【发布时间】: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


【解决方案1】:
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

应该纠正你的问题

实现它的更好方法是:

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) 
#print(Questions_List)

for i in range(0,len(Questions_List)):
  answer = input(Questions_List[i][0])
  if answer == Questions_List[i][1]:
    print("Correct!")
    score += 1
  else:
    incorrect_answers += 1
    print("Let's try again")
    continue

print("Your final score is: ",score)

【讨论】:

    【解决方案2】:

    更好的方法:

    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-incorrect_answers)<len(Questions_List):
       a = answer(Questions_List, score)
       if a == Questions_List[score][1]:
           print ("Correct!")
           score+=1
       elif a != Questions_List[score][1]:
           print ("Incorrect!")
           incorrect_answers+=1
    

    【讨论】:

      猜你喜欢
      • 2017-06-25
      • 2023-03-03
      • 2012-12-13
      • 1970-01-01
      • 2011-04-23
      • 1970-01-01
      • 2022-01-14
      • 2013-09-11
      • 2013-03-14
      相关资源
      最近更新 更多