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

Time:04-20

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()

CodePudding user response:

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)

CodePudding user response:

Samwise is right that there is an issue with the reassignment of points at every iteration of the for loop. However, you can make your code a bit cleaner by storing all of the question-answer pairs in a list of tuples, and then iterating over them one at a time to collect answers from the user:

def ask_questions():
    quiz_questions = [
        ('1) Who was the first American woman in space? ', 'Sally Ride'),
        ('2) True or false: 5 kilometer == 5000 meters? ', 'true'),
        ('3) (5   3)/2 * 10 = ? ', '40'),
        ("4) Given the list [8, 'Orbit', 'Trajectory', 45], what entry is at index 2? ", 'Trajectory'),
        ('5) What is the minimum crew size for the ISS? ', '3')
    ]
    submitted_answers = []
    
    for question, _ in quiz_questions:
        submitted_answers.append(input(question))
        
    points = 0
    for answer, (_, correct_answer) in zip(submitted_answers, quiz_questions):
        if answer.lower() == correct_answer.lower():
            points  = 1
            print(f'Your Answer: {answer}\nCorrect Answer: {correct_answer}') 
        else:
            print('Incorrect.\nThe correct answer is: ', correct_answer)
    print(points)

CodePudding user response:

You declared points inside the loop, problem with that is with every iretation, points get reset back to 0 and to fix that just moved it outside of the for loop.

from:

for item in quiz_questions:
    points = 0
    ...

to:

points = 0
for item in quiz_questions:
    ...

However I do think your code could be written a little cleaner:

questions = [
    {
        "question": '1) Who was the first American woman in space? ',
        "answer": 'Sally Ride'
    },
    {
        "question": '2) True or false: 5 kilometer == 5000 meters? ',
        "answer": 'true'
    },
    {
        "question": '3) (5   3)/2 * 10 = ? ',
        "answer": '40'
    },
    {
        "question": "4) Given the list [8, 'Orbit', 'Trajectory', 45], what entry is at index 2? ",
        "answer": 'Trajectory'
    },
    {
        "question": '5) What is the minimum crew size for the ISS? ',
        "answer": '3'
    },
]

points = 0
for i in questions:
    if input(i['question']).lower() == i['answer'].lower():
        print('correct')
    else:
        print(f"Incorrect.\nThe correct answer is: {i['answer']}")
    print(points)

  • Related