gpt4 book ai didi

python - 元组索引超出范围? python 3.4.2

转载 作者:太空宇宙 更新时间:2023-11-03 17:52:44 25 4
gpt4 key购买 nike

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

获得百分比后,出现错误

print("Q{0}: {1}".format(data[question][0]))
IndexError: tuple index out of range

有什么想法吗?

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

最佳答案

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

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

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

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

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

关于python - 元组索引超出范围? python 3.4.2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28912700/

25 4 0
文章推荐: python - 通过电子邮件和 django 数据库中的密码进行用户身份验证
文章推荐: ruby - 为什么除以 1.8 不起作用,但乘以 (5.0/9) 却起作用?
文章推荐: javascript -
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com