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

Quiz Project: Answers expected to evaluate to True

I am currently working on quiz.py (I haven't watched the answer video yet). I hit a roadblock where in the ask method, I compare the input for the question answer from a string to an integer and then compare to the question's answer (question.answer). I print out the answer from input and the question's answer to verify they're the same and they appear to be the same but all correct answers evaluate to False when I compare answer == question.answer.

Here is the complete code and a trial run:

questions.py

class Question:
    answer = None
    text = None

class Add(Question):
    def __init__(self, num1, num2):
        self.text = '{} + {}'.format(num1, num2)
        self.answer = num1 + num2

class Multiply(Question):
    def __init__(self, num1, num2):
        self.text = '{} x {}'.format(num1, num2)
        self.answer = num1 * num2

quiz.py

import datetime
import random

from questions import Add, Multiply

class Quiz:
        questions = []
        answers = []

        def __init__(self):
                # generate 10 random questions with numbers from 1 to 10
                # add these questions into self.questions
                for x in range(0, 10):
                        num1 = random.randint(1, 10)
                        num2 = random.randint(1, 10)
                        question_type = random.randint(1, 2)
                        if question_type == 1:
                                self.questions.append(Add(num1, num2))
                        else:
                                self.questions.append(Multiply(num1, num2))


        def take_quiz(self):
                # log the start time
                # start = datetime.datetime.now()
                # ask all of the questions
                for i in range(len(self.questions)):
                        answer = self.ask(self.questions[i])
                # log if they got the question right
                        self.answers.append(answer)
                # log the end time
                # show a summary
                self.summary()

        def ask(self, question):
                # log the start time
                # start = datetime.datetime.now()
                # capture the answers
                answer = input(question.text + ": ")
                int(answer)
                print(answer)
                print(question.answer)                
                # check the answers
                if answer == question.answer:
                        return True
                else:
                        return False
                # log the end time
                # if the answer's right, send back True
                # othrwise, send back False
                # send back the elapsed time, too

        def summary(self):
                for answer in self.answers:
                        print(str(answer))
                # print how many you got right and the total # of questions. 5/10
                # print the total time for the quiz: 30 seconds!

Trial run

>>> q = Quiz()
>>> q.take_quiz()
1 x 7: 7
7
7
3 x 5: 15
15
15
6 + 2: 8
8
8
7 x 5: 35
35
35
2 + 4: 6
6
6
8 + 2: 10
10
10
2 x 7: 14
14
14
1 x 6: 6
6
6
6 + 3: 9
9
9
8 + 1: 9
9
9
False
False
False
False
False
False
False
False
False
False

2 Answers

Based on your output, I'm betting the answer is in your ask() method.

               answer = input(question.text + ": ")
                int(answer)
                print(answer)
                print(question.answer)                
                # check the answers
                if answer == question.answer:
                        return True
                else:
                        return False

answer is a string since it's what you grabbed from an input. The answer from question.answer is an int. You tried to make the input answer an int but didn't assign it to anything, so answer is still a string. That means that your if-condition will always ask if a string is equivalent to an int, which will always return False. Otherwise, as someone who is past the answer video, I have you say your code is very similar to what Kenneth wrote (I'm guessing you'll add the datetime parts later).

Indeed, you are correct. Thank you for pointing that out. Changing the line

int(answer)

to

answer = int(answer)

reassigns the typecast value to the variable and the equivalences can be evaluated.