I know my code is messy bear with me. I just want to get it working and then I will make it clean. Yes I know some parts aren't finished but It keeps saying index is out of range and I cant figure out where to put 1's and stuff like that to make it all line up. I know its an easy fix i'm just not sure what I have to change to make it index correctly.
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()
CodePudding user response:
Here's a start with minimal changes, although your arrays are of size 20 and you loop only 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()
CodePudding user response:
Storing the correct and incorrect answers in separate lists makes it impossible to produce the report that you want to produce at the end, since you lose any record of which number each answer was -- all three lists will have different indices corresponding to the same question, so trying to use an index from one list in another list will get you either the wrong entry or an IndexError
almost every time.
You could fix this by using two dictionaries instead of two lists (so you can make the keys in the dictionaries match the indices in the main answer list), but I suggest just storing a single list of the user's answers, which makes it easy to zip the correct answers with the user's answers:
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}')
CodePudding user response:
The following minor changes let your code run to completion. The report at the end does not actually print what you say it will print, but I'll let you finish that part.
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()
Output:
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
I don't understand why you're printing the "number right" and "number wrong" as floats. Those will always be integers.