Home > Enterprise >  Array not lining up correctly - PYTHON
Array not lining up correctly - PYTHON

Time:04-20

Okay I'm back again. I asked last week about some issues I was having with arrays and I fixed the problem but I'm running into a similar problem. My friend's code looks EXACTLY like mine like no joke almost identical in format but his works and mine does not. Everything works except for the very end when it shows the right answer and then what the user entered. If this seems like a weird way to do this, I know its how I'm supposed to, don't badger me for it haha. I get an indexing error but I'm not sure why. It must be something simple and similar like last week.

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 ammount correct variable
    amount_correct = 0
    # Initializes the ammount 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()

CodePudding user response:

Basically you stored questions_input as a string that kept changing, so you at the end you were trying to find the 20th character in the string "A" for example. I created a new array where the users answers are stored and then those are used at the end.

# 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 ammount correct variable
amount_correct = 0
# Initializes the ammount 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])
  • Related