Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Dates and Times in Python (2014) Let's Build a Timed Quiz App Taking The Quiz

Keep getting: You got 1 out of 10 right.

Can someone help me out here? Thanks!

import datetime
import random

from questions import Add, Multiply


class Quiz:
    questions = []
    answers = []

    def __init__(self):
        question_types = (Add, Multiply)
        for _ in range(10):  # Generate 10 random questions with numbers from 1 to 10
            num1 = random.randint(1, 10)
            num2 = random.randint(1, 10)
            question = random.choice(question_types)(num1, num2)
            self.questions.append(question)  # Add these questions into self.questions

    def take_quiz(self):
        # Log the start time
        self.start_time = datetime.datetime.now()
        # Ask all the questions
        for question in self.questions:
            # Log if they got the questions right
            self.answers.append(self.ask(question))
        # Log the end time
        else:
            self.end_time = datetime.datetime.now()
        # Show a summary
        return self.summary()

    def ask(self, question):
        correct = False
        # Log the start time
        question_start = datetime.datetime.now()
        # Capture the answer
        answer = input(question.text + ' = ')
        # Check the answer
        if int(answer) == question.answer:
            correct = True
        # Log the end time
        question_end = datetime.datetime.now()
        # If the answer's right, send back True
        # otherwise, send back False
        # send back the elapsed time
        return correct, question_end - question_start

    def total_correct(self):
        # Return the total number of correct answers
        total = 0
        for answer in self.answers:
            if answer[0]:
                total += 1
            return total

    def summary(self):
        # Print how many you got right and the total number of questions. ex: 5/10
        print("You got {} out of {} right.".format(self.total_correct(), len(self.questions)))
        # Print the total time for the quiz: 30 seconds!
        print("It took you {} seconds to complete!".format((self.end_time - self.start_time).seconds))

Quiz().take_quiz()

1 Answer

Steven Parker
Steven Parker
231,269 Points

The return at the end of the total_correct method is indented too far, putting it inside the loop and causing the method to end in the first pass of the loop. So only the first answer gets counted.

Change the indent so the loop can finish before the method returns.

Thank you!