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

Always 0/10 Quiz.py - mistake

Good Evening,

The quiz shows always that I have 0/10 correct. What is wrong here? I have used the right Python version, so that's not the mistake;) Can somebody help me?

import datetime
import random

from questions import Add, Multiply



class Quiz:
    questions = []
    answers = []

    def __init__(self):
        questions_type = (Add, Multiply)
        # generate ten random questions with numbers from 1 to 10
        for _ in range(10):
            num1 = random.randint(1,10)
            num2 = random.randint(1,10)
            question = random.choice(questions_type)(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 of the questions
        for question in self.questions:
            # log if they got the question right
            self.answers.append(self.ask(question))
        else:
            # log the end time
            self.endtime = datetime.datetime.now()
            # show a summary
        return self.summary()

    def ask(self, questions):
        correct = False
        # log the start time
        question_start = datetime.datetime.now()
        # capture the answer
        answer=input(questions.text + " = ")
        # check the answer
        if answer == str(questions.answer):
            correct == True
        # log the end time
        question_end = datetime.datetime.now()

        # if the answer is right, send back True
        # else, send back False
        # send back the elapsed time too
        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]: # True/False
                total+=1
        return total



    def summary(self):
        # print how many you got right and the total of questions like 8/10
        print("Yoi got {} out of {} right.".format(
            self.total_correct(), len(self.questions)
        ))
        # print total time for the quiz
        print("It took you {} seconds total.".format(
            (self.endtime-self.start_time).seconds
        ))

Quiz().take_quiz()

Thanks in Advance

P.S. Sorry for so many questions

1 Answer

Here I found the error, I changed the correct == True for correct = True:

def ask(self, questions):
        correct = False
        # log the start time
        question_start = datetime.datetime.now()
        # capture the answer
        answer=input(questions.text + " = ")
        # check the answer
        if answer == str(questions.answer):
            correct = True   #changed double == for one =
        # log the end time
        question_end = datetime.datetime.now()

        # if the answer is right, send back True
        # else, send back False
        # send back the elapsed time too
        return correct, question_end - question_start

Always this little mistakes;) Thank you very much!:)