元组索引超出范围?python 3.4.2

元组索引超出范围?python 3.4.2,python,csv,python-3.4,Python,Csv,Python 3.4,我正在用python创建一个csv文件测验,在测验结束时看到总结之前一切都很好 percentage = (correctScore * 100) / 20 print("Alright now, lets see how you did... ") time.sleep(2) if correctScore == 20: print("Excellent work there, (yourNameIs), you scored 100%. ") elif correctScore &

我正在用python创建一个csv文件测验,在测验结束时看到总结之前一切都很好

percentage = (correctScore * 100) / 20

print("Alright now, lets see how you did... ")
time.sleep(2)
if correctScore == 20:
    print("Excellent work there, (yourNameIs), you scored 100%. ")
elif correctScore >= 11 <= 19:
    print("Well done", yourNameIs, "you passed. You scored", percentage,"%. You got {} question(s) wrong".format(len(incorrect)))
    for question, user_answer in incorrect:
        print("Q{}: {}".format(data[question][0]))
        print("\tYou answered {}. The correct answer is {}".format(user_answer, data[question][5]))
elif correctScore >= 5 <= 10:
    print("Good effort", yourNameIs, "you scored", percentage,"%. You got {} question(s) wrong".format(len(incorrect)))
    for question, user_answer in incorrect:
        print("Q{}: {}".format(data[question][0]))
        print("\tYou answered {}. The correct answer is {}".format(user_answer, data[question][5]))
else:
    print("You need to try harder next time", yourNameIs, "you scored", percentage,"%. You got {} question(s) wrong".format(len(incorrect)))
    for question, user_answer in incorrect:
        print("Q{}: {}".format(data[question][0]))
        print("\tYou answered {}. The correct answer is {}".format(user_answer, data[question][5]))
有什么想法吗

correctScore = 0 
incorrect = [] 
question = data[recordnum-1][0] 
a = data[recordnum-1][1] 
b = data[recordnum-1][2] 
c = data[recordnum-1][3] 
d = data[recordnum-1][4] 
answer = data[recordnum-1][5] –

在格式字符串中使用两个
{}
,但只传递一个arg,python需要两个参数进行格式化,因此将
索引器:元组索引超出范围

print("Q{0}: {1}".format(data[question][0]))  
        ^^    ^^
如果
数据[问题]
有两个元素,您可以使用
*
解包:

print("Q{0}: {1}".format(*data[question]))

无论哪种方式,您都需要通过两个参数。

1。请告诉我们
数据
在哪里分配,以及分配的代码。2.给我一个输入数据格式的例子。不仅仅是csv,而是一个条目的外观like@Vincenzzochi很抱歉下面是我得到的更正分数=0不正确=[]myFile=open(“questions.csv”,“r”)#打开csv文件并将其存储在数组myFile lines=myFile.readlines()#将csv文件的行读取到变量players recordnum=1 random中。shuffle(lines)data=[p.strip().split(“,”)表示p in line]
question
也是一个数字,还是一个字符串?我们不想发送那个^。correctScore=0不正确=[]问题=数据[recordnum-1][0]a=数据[recordnum-1][1]b=数据[recordnum-1][2]c=数据[recordnum-1][3]d=数据[recordnum-1][4]答案=数据[recordnum-1][5]将此代码包含在问题中。
print("Q{0}: {1}".format(*data[question]))