Python Dates and Times in Python Let's Build a Timed Quiz App The Quiz Class

Sahar Nasiri
Sahar Nasiri
7,454 Points

IndentationError

Why do I get IndentationError on def ask line? Here is my code:

import datetime
import random

from questions import Add, Multiply

class Quiz():
    questions = []
    answers = []

    def __init__(self):
        question_types = (Add, Multiply)
        #queston_type[0](1, 5)
        # generate 10 random questions with numbers from 1 to 10
        for _ in range(10):
            num1 = random.radint(1, 10)
            num2 = random.randint(1, 10)
            question = random.choice(question_types)
            # add these questions into self.questions
            self.questions.append(question)

    def take_quiz(self):
        # log the start time
        # ask all of the questions
        # log if they got the question right
        # log the end time
        # show a summary


    def ask(self, question):
    # log the start time
    # capture the answer
        # check the answer
        # log the end time
        # if the answer's right, send back True
        # otherwise, send back False
        # send back the elapsed time, too

    def total_correct(self):
        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 # of questions. 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 total".format(
            (self.end_time - self.start_time).seconds  
        ))

1 Answer

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 59,892 Points

A block of code can not consist of just comments. If you define a function the minimum code block is the built-in keyword pass. Other fixed in comments below:

import datetime
import random

from questions import Add, Multiply


class Quiz():
    questions = []
    answers = []

    def __init__(self):
        question_types = (Add, Multiply)
        # queston_type[0](1, 5)
        # generate 10 random questions with numbers from 1 to 10
        for _ in range(10):
            num1 = random.radint(1, 10)
            num2 = random.randint(1, 10)
            question = random.choice(question_types)
            # add these questions into self.questions
            self.questions.append(question)

    def take_quiz(self):
        # log the start time
        # ask all of the questions
        # log if they got the question right
        # log the end time
        # show a summary
        pass  # <-- Added pass

    def ask(self, question):
        # log the start time
        # capture the answer
        # check the answer
        # log the end time
        # if the answer's right, send back True
        # otherwise, send back False
        # send back the elapsed time, too
        pass  # <-- Added pass

    def total_correct(self):
        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 # of questions. 5/10
        print("You got {} out of {} right!".format(
            self.total_correct(), len(self.questions
        )))  # <-- added right paren
        # print the total time for the quiz: 30 seconds!
        print("It took you {} seconds total".format(
            (self.end_time - self.start_time).seconds
        ))