阵列未正确排列 - PYTHON

Array not lining up correctly - PYTHON

好的,我又回来了。上周我询问了我在使用数组时遇到的一些问题,我解决了这个问题,但我 运行 遇到了类似的问题。我朋友的代码看起来和我的完全一样,这不是开玩笑,在格式上几乎完全相同,但他的作品和我的不一样。一切正常,除了最后显示正确答案然后显示用户输入的内容。如果这看起来是一种奇怪的方式来做到这一点,我知道我应该怎么做,不要因为它而纠缠我哈哈。我收到索引错误,但不确定原因。一定是和上周一样简单又类似的东西。

def main():

    # Initializes answer sheet
    questions_answers = ['B', 'D', 'A', 'A', 'C', "A", 'B', 'A', 'C', 'D', 'B', 'C', 'D', 'A', 'D', 'C', 'C', 'B', 'D', 'A', ]
    # Initializes the array for the questions the user got wrong
    questions_wrong = []
    # Initializes the amount correct variable
    amount_correct = 0
    # Initializes the amount incorrect variable
    amount_incorrect = 0

    # Starts a for loop to get users answers
    for question_number in range(20):
        # Asks user to input their answer
        questions_input = input('Enter Your answer for Question #' + str(question_number + 1) + ': ')
        # Sets inputs to uppercase
        questions_input = questions_input.upper()

        # If statement to determine if input is right
        if questions_input == questions_answers[question_number]:
            amount_correct += 1
        # Else statement to add 1 to incorrect count and the answer to the list of questions wrong
        else:
            questions_wrong.append((question_number + 1, questions_input))
            amount_incorrect += 1

    # Prints blank line
    print()

    # If statement to determine pass
    if amount_correct >= 15:
        # Prints thta the user passed
        print('**YOU PASSED**')
    # Else statement to determine fail
    else:
        # Prints the user failed
        print('**YOU FAILED**')

    # Prints the number correct
    print('Number correct: ', amount_correct)

    # Pirnts the number incorrect
    print('Number incorrect:', amount_incorrect)
    # Prints 3 blank lines and headers for comparing wrong answers
    print()
    print()
    print()
    print('You got the following questions wrong:')
    print()
    print('Question     Correct     Your Answer')
    print('--------     -------     -----------')
    # Runs a for loop to display what the user got wrong and what the right answer was
    for question_number in range(20):
        if questions_answers[question_number] != questions_input[question_number]:
            print('  ', question_number + 1, '         ', questions_answers[question_number], '           ', questions_input[question_number])

# End of main procedure
main()

基本上你将 questions_input 存储为一个不断变化的字符串,所以你最后试图在字符串“A”中找到第 20 个字符,例如。我创建了一个新数组,其中存储了用户的答案,然后在最后使用这些答案。

# Initializes answer sheet
questions_answers = ['B', 'D', 'A', 'A', 'C', "A", 'B', 'A', 'C', 'D', 'B', 'C', 'D', 'A', 'D', 'C', 'C', 'B', 'D', 'A', ]
# Initializes the array to store the answers the User enters 
user_answers = []
# Initializes the array for the questions the user got wrong
questions_wrong = []
# Initializes the amount correct variable
amount_correct = 0
# Initializes the amount incorrect variable
amount_incorrect = 0

# Starts a for loop to get users answers
for question_number in range(20):
    # Asks user to input their answer
    questions_input = input('Enter Your answer for Question #' + str(question_number + 1) + ': ')
    # Sets inputs to uppercase
    questions_input = questions_input.upper()
    user_answers.append(questions_input)

    # If statement to determine if input is right
    if questions_input == questions_answers[question_number]:
        amount_correct += 1
    # Else statement to add 1 to incorrect count and the answer to the list of questions wrong
    else:
        questions_wrong.append((question_number + 1, questions_input))
        amount_incorrect += 1

# Prints blank line
print()

# If statement to determine pass
if amount_correct >= 15:
    # Prints thta the user passed
    print('**YOU PASSED**')
# Else statement to determine fail
else:
    # Prints the user failed
    print('**YOU FAILED**')

# Prints the number correct
print('Number correct: ', amount_correct)

# Pirnts the number incorrect
print('Number incorrect:', amount_incorrect)
# Prints 3 blank lines and headers for comparing wrong answers
print()
print()
print()
print('You got the following questions wrong:')
print()
print('Question     Correct     Your Answer')
print('--------     -------     -----------')
# Runs a for loop to display what the user got wrong and what the right answer was
for question_number in range(20):
    if questions_answers[question_number] != user_answers[question_number]:
        print('  ', question_number + 1, '         ', questions_answers[question_number], '           ', user_answers[question_number])