Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python Dates and Times in Python 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
216,071 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!