元组索引超出范围?python 3.4.2

2024-10-01 00:26:41 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在用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]))

在我得到我的百分比后,我得到了一个错误

^{pr2}$

有什么想法吗?在

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] –

Tags: answeryouformatdataquestionprintgotpercentage
1条回答
网友
1楼 · 发布于 2024-10-01 00:26:41

在格式字符串中使用两个{},但只传递一个参数,python需要两个参数进行格式化,因此得到IndexError: tuple index out of range

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

如果data[question]有两个元素,则可以使用*解包:

^{pr2}$

不管怎样,你都需要传递两个参数。在

相关问题 更多 >