Skip to content
Advertisement

Trying to add up points for correct answers in a quiz in Python

I am trying to get a program to add up points for correct answers. I am supposed to have my questions, correct answers, and candidate answers in three separate lists. The questions are to be asked one at a time and I have to return whether or not the answers are correct. I am then supposed to add up the points for each correct answer. When I run my code, it is only displaying 1 after a correct answer. I have tried putting the points variable in different places to see if that will work I have also put the “points +=” variable in different places thinking that it might be a logic problem but it still won’t do what I need it to do.

My function is below.

def ask_questions():
#create lists for questions, correct answers, and candidate answers
    question_1 = '1) Who was the first American woman in space? '
    correct_answer_1 = 'Sally Ride'
    candidate_answer_1 = input(question_1)
    
    question_2 = '2) True or false: 5 kilometer == 5000 meters? '
    correct_answer_2 = 'true'
    candidate_answer_2 = input(question_2)
    
    question_3 = '3) (5 + 3)/2 * 10 = ? '
    correct_answer_3 = '40'
    candidate_answer_3 = input(question_3)
    
    question_4 = "4) Given the list [8, 'Orbit', 'Trajectory', 45], what entry is at index 2? "
    correct_answer_4 = 'Trajectory'
    candidate_answer_4 = input(question_4)
    
    question_5 = '5) What is the minimum crew size for the ISS? '
    correct_answer_5 = '3'
    candidate_answer_5 = input(question_5)
    
    #lists that correlate to the variables assigned above
    
    quiz_questions = [question_1, question_2, question_3, question_4, question_5]
    correct_answers = [correct_answer_1, correct_answer_2, correct_answer_3, correct_answer_4, correct_answer_5]
    candidate_answers = [candidate_answer_1, candidate_answer_2, candidate_answer_3, candidate_answer_4, candidate_answer_5]
    
    index = 0
    # points = 0
    # total_score = (points/5) * 100
    for item in quiz_questions:
        points = 0
        if candidate_answers[index].lower() == correct_answers[index].lower():
            points += 1
            print(f'Your Answer: {candidate_answers[index]}nCorrect Answer: {correct_answers[index]}') 
        elif candidate_answers[index] != correct_answers[index]:
            print('Incorrect.nThe correct answer is: ', correct_answers[index])
        index += 1
        print(points)
        
    
ask_questions()

Advertisement

Answer

The main problem is that you’re resetting points to 0 inside the loop, which means that can only ever be either 0 or 1. The business with index is confusing and might be making it difficult to debug the points stuff; I suggest just using zip instead to make the whole thing easier:

points = 0
for correct, candidate in zip(correct_answers, candidate_answers):
    if correct.lower() == candidate.lower():
        points += 1
        print(f'Your Answer: {candidate}nCorrect Answer: {correct}') 
    else:
        print('Incorrect.nThe correct answer is: ', correct)
print(points)
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement