无法正确索引
Cant get to index correctly
我知道我的代码很乱,请耐心等待。我只是想让它工作,然后我会把它弄干净。是的,我知道有些部分还没有完成,但它一直说索引超出范围,我无法弄清楚在哪里放置 + 1 之类的东西以使其全部对齐。我知道这是一个简单的修复方法,我只是不确定我必须更改什么才能使其正确索引。
def main():
questions_answers = ['B', 'D', 'A', 'A', 'C', "A", 'B', 'A', 'C', 'D', 'B', 'C', 'D', 'A', 'D', 'C', 'C', 'B', 'D', 'A', ]
questions_input = [''] * 20
questions_wrong = ['']
amount_correct = 0
amount_incorrect = 0
question_number = 1
while question_number <= 10:
questions_input = input('Enter Your answer for Question #' + str(question_number + 1) +': ')
questions_input = questions_input.upper
question_number += 1
if questions_input == questions_answers[question_number]:
amount_correct + 1
else:
questions_wrong.append(questions_input + 1)
amount_incorrect
print()
if amount_correct >= 15:
print('**YOU PASSED**')
else:
print('**YOU FAILED**')
print('Number correct: ', format(amount_correct, '.2f'))
print('Number incorrect:', format(amount_incorrect, '.2f'))
print()
print()
print()
print('You got the following questions wrong:')
print()
print('Question Correct Your Answer')
print('-------- ------- -----------')
print(questions_wrong)
print(questions_input)
main()
下面的小改动让你的代码运行完成。最后的报告实际上并没有打印你说的会打印的内容,但我会让你完成那部分。
def main():
questions_answers = ['B', 'D', 'A', 'A', 'C', "A", 'B', 'A', 'C', 'D', 'B', 'C', 'D', 'A', 'D', 'C', 'C', 'B', 'D', 'A', ]
questions_wrong = []
amount_correct = 0
amount_incorrect = 0
for question_number in range(10):
questions_input = input('Enter Your answer for Question #' + str(question_number + 1) +': ')
questions_input = questions_input.upper()
if questions_input == questions_answers[question_number]:
amount_correct += 1
else:
questions_wrong.append((question_number+1,questions_input))
amount_incorrect += 1
print()
if amount_correct >= 15:
print('**YOU PASSED**')
else:
print('**YOU FAILED**')
print('Number correct: ', format(amount_correct, '.2f'))
print('Number incorrect:', format(amount_incorrect, '.2f'))
print()
print()
print()
print('You got the following questions wrong:')
print()
print('Question Correct Your Answer')
print('-------- ------- -----------')
print(questions_wrong)
print(questions_input)
main()
输出:
Enter Your answer for Question #1: q
Enter Your answer for Question #2: d
Enter Your answer for Question #3: q
Enter Your answer for Question #4: q
Enter Your answer for Question #5: q
Enter Your answer for Question #6: q
Enter Your answer for Question #7: q
Enter Your answer for Question #8: q
Enter Your answer for Question #9: q
Enter Your answer for Question #10: q
**YOU FAILED**
Number correct: 1.00
Number incorrect: 9.00
You got the following questions wrong:
Question Correct Your Answer
-------- ------- -----------
[(0, 'Q'), (2, 'Q'), (3, 'Q'), (4, 'Q'), (5, 'Q'), (6, 'Q'), (7, 'Q'), (8, 'Q'), (9, 'Q')]
Q
我不明白你为什么要把“数字正确”和“数字错误”打印成浮点数。这些将永远是整数。
尽管您的数组大小为 20 而您仅循环 10,但这里是一个最小更改的开始
def main():
questions_answers = ['B', 'D', 'A', 'A', 'C', "A", 'B', 'A', 'C', 'D', 'B', 'C', 'D', 'A', 'D', 'C', 'C', 'B', 'D', 'A', ]
questions_input = [''] * 20
questions_wrong = [''] * 20
amount_correct = 0
amount_incorrect = 0
question_number = 0
while question_number < 10:
questions_input[question_number] = input('Enter Your answer for Question #' + str(question_number + 1) +': ')
if questions_input[question_number] == questions_answers[question_number]:
amount_correct += 1
else:
questions_wrong[question_number] = questions_input[question_number]
amount_incorrect += 1
question_number += 1
print()
if amount_correct >= 15:
print('**YOU PASSED**')
else:
print('**YOU FAILED**')
print('Number correct: ', format(amount_correct, '.2f'))
print('Number incorrect:', format(amount_incorrect, '.2f'))
print()
print()
print()
print('You got the following questions wrong:')
print()
print('Question Correct Your Answer')
print('-------- ------- -----------')
print(questions_wrong)
print(questions_input)
main()
将正确和不正确的答案存储在单独的列表中会导致无法生成您希望在最后生成的报告,因为您会丢失每个答案的任何记录——所有三个列表将具有不同的索引对应于相同的问题,因此尝试在另一个列表中使用一个列表中的索引几乎每次都会让您输入错误或 IndexError
。
你可以通过使用两个字典而不是两个列表来解决这个问题(这样你就可以使字典中的键与主答案列表中的索引相匹配),但我建议仅存储用户答案的单个列表,这使得将正确答案与用户答案压缩在一起变得容易:
correct_answers = list('BDAACABACDBCDADCCBDA')
user_answers = [
input(f'Enter Your answer for Question #{n}: ').upper()
for n in range(1, len(correct_answers) + 1)
]
num_correct = sum(c == u for c, u in zip(correct_answers, user_answers))
num_incorrect = len(user_answers) - num_correct
if num_correct >= 15:
print('**YOU PASSED**')
else:
print('**YOU FAILED**')
print(f'Number correct: {num_correct}')
print(f'Number incorrect: {num_incorrect}')
print()
print('You got the following questions wrong:')
print()
print('Question Correct Your Answer')
print('-------- ------- -----------')
for n, (c, u) in enumerate(zip(correct_answers, user_answers), 1):
print(f'{n:<13}{c:<12}{u:<11}')
我知道我的代码很乱,请耐心等待。我只是想让它工作,然后我会把它弄干净。是的,我知道有些部分还没有完成,但它一直说索引超出范围,我无法弄清楚在哪里放置 + 1 之类的东西以使其全部对齐。我知道这是一个简单的修复方法,我只是不确定我必须更改什么才能使其正确索引。
def main():
questions_answers = ['B', 'D', 'A', 'A', 'C', "A", 'B', 'A', 'C', 'D', 'B', 'C', 'D', 'A', 'D', 'C', 'C', 'B', 'D', 'A', ]
questions_input = [''] * 20
questions_wrong = ['']
amount_correct = 0
amount_incorrect = 0
question_number = 1
while question_number <= 10:
questions_input = input('Enter Your answer for Question #' + str(question_number + 1) +': ')
questions_input = questions_input.upper
question_number += 1
if questions_input == questions_answers[question_number]:
amount_correct + 1
else:
questions_wrong.append(questions_input + 1)
amount_incorrect
print()
if amount_correct >= 15:
print('**YOU PASSED**')
else:
print('**YOU FAILED**')
print('Number correct: ', format(amount_correct, '.2f'))
print('Number incorrect:', format(amount_incorrect, '.2f'))
print()
print()
print()
print('You got the following questions wrong:')
print()
print('Question Correct Your Answer')
print('-------- ------- -----------')
print(questions_wrong)
print(questions_input)
main()
下面的小改动让你的代码运行完成。最后的报告实际上并没有打印你说的会打印的内容,但我会让你完成那部分。
def main():
questions_answers = ['B', 'D', 'A', 'A', 'C', "A", 'B', 'A', 'C', 'D', 'B', 'C', 'D', 'A', 'D', 'C', 'C', 'B', 'D', 'A', ]
questions_wrong = []
amount_correct = 0
amount_incorrect = 0
for question_number in range(10):
questions_input = input('Enter Your answer for Question #' + str(question_number + 1) +': ')
questions_input = questions_input.upper()
if questions_input == questions_answers[question_number]:
amount_correct += 1
else:
questions_wrong.append((question_number+1,questions_input))
amount_incorrect += 1
print()
if amount_correct >= 15:
print('**YOU PASSED**')
else:
print('**YOU FAILED**')
print('Number correct: ', format(amount_correct, '.2f'))
print('Number incorrect:', format(amount_incorrect, '.2f'))
print()
print()
print()
print('You got the following questions wrong:')
print()
print('Question Correct Your Answer')
print('-------- ------- -----------')
print(questions_wrong)
print(questions_input)
main()
输出:
Enter Your answer for Question #1: q
Enter Your answer for Question #2: d
Enter Your answer for Question #3: q
Enter Your answer for Question #4: q
Enter Your answer for Question #5: q
Enter Your answer for Question #6: q
Enter Your answer for Question #7: q
Enter Your answer for Question #8: q
Enter Your answer for Question #9: q
Enter Your answer for Question #10: q
**YOU FAILED**
Number correct: 1.00
Number incorrect: 9.00
You got the following questions wrong:
Question Correct Your Answer
-------- ------- -----------
[(0, 'Q'), (2, 'Q'), (3, 'Q'), (4, 'Q'), (5, 'Q'), (6, 'Q'), (7, 'Q'), (8, 'Q'), (9, 'Q')]
Q
我不明白你为什么要把“数字正确”和“数字错误”打印成浮点数。这些将永远是整数。
尽管您的数组大小为 20 而您仅循环 10,但这里是一个最小更改的开始
def main():
questions_answers = ['B', 'D', 'A', 'A', 'C', "A", 'B', 'A', 'C', 'D', 'B', 'C', 'D', 'A', 'D', 'C', 'C', 'B', 'D', 'A', ]
questions_input = [''] * 20
questions_wrong = [''] * 20
amount_correct = 0
amount_incorrect = 0
question_number = 0
while question_number < 10:
questions_input[question_number] = input('Enter Your answer for Question #' + str(question_number + 1) +': ')
if questions_input[question_number] == questions_answers[question_number]:
amount_correct += 1
else:
questions_wrong[question_number] = questions_input[question_number]
amount_incorrect += 1
question_number += 1
print()
if amount_correct >= 15:
print('**YOU PASSED**')
else:
print('**YOU FAILED**')
print('Number correct: ', format(amount_correct, '.2f'))
print('Number incorrect:', format(amount_incorrect, '.2f'))
print()
print()
print()
print('You got the following questions wrong:')
print()
print('Question Correct Your Answer')
print('-------- ------- -----------')
print(questions_wrong)
print(questions_input)
main()
将正确和不正确的答案存储在单独的列表中会导致无法生成您希望在最后生成的报告,因为您会丢失每个答案的任何记录——所有三个列表将具有不同的索引对应于相同的问题,因此尝试在另一个列表中使用一个列表中的索引几乎每次都会让您输入错误或 IndexError
。
你可以通过使用两个字典而不是两个列表来解决这个问题(这样你就可以使字典中的键与主答案列表中的索引相匹配),但我建议仅存储用户答案的单个列表,这使得将正确答案与用户答案压缩在一起变得容易:
correct_answers = list('BDAACABACDBCDADCCBDA')
user_answers = [
input(f'Enter Your answer for Question #{n}: ').upper()
for n in range(1, len(correct_answers) + 1)
]
num_correct = sum(c == u for c, u in zip(correct_answers, user_answers))
num_incorrect = len(user_answers) - num_correct
if num_correct >= 15:
print('**YOU PASSED**')
else:
print('**YOU FAILED**')
print(f'Number correct: {num_correct}')
print(f'Number incorrect: {num_incorrect}')
print()
print('You got the following questions wrong:')
print()
print('Question Correct Your Answer')
print('-------- ------- -----------')
for n, (c, u) in enumerate(zip(correct_answers, user_answers), 1):
print(f'{n:<13}{c:<12}{u:<11}')