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
Joshua Armstrong
4,921 PointsQuiz.py is returning the wrong number of correct answers. I have scoured my code and cannot find the issue.
So this is my first step in python aside from codecademy and I've been going along trying to do most things on my own and really get a good grasp of python and I feel like I'm doing well. However in the timed quiz app, I followed along with Kenneth Love in the videos and my app works but my score comes back wrong. I can't find the error. Could someone take a look at it and let me know what you think? I'm pretty sure it's in my Quiz class.
import datetime
import random
from questions import Add, Multiply, Subtract
class Quiz:
questions = []
answers = []
def __init__(self):
question_types = (Add, Multiply)
#generate 10 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(question_types)(num1, num2)
#add these questions into self.questions
self.questions.append(question)
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.end_time = datetime.datetime.now()
#show a sumary
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 answer == str(question.answer):
correct = True
# log the end time
question_end = datetime.datetime.now()
# if the anwer is right, send back true
# otherwise send back false
# send back the elapsed time
return correct, question_end - question_start
def total_correct(self):
# return total # 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 total # of questions. 5/10
print("You got {} out of {} right.".format(
self.total_correct(), len(self.questions)
))
print("it took you {} seconds total.".format(
(self.end_time-self.start_time).seconds
))
Quiz().take_quiz()
[MOD: aded ```python formatting -cf]
Kenneth Love
Treehouse Guest TeacherI haven't scoured your code (yet), but I did notice that you have answer [0] instead of answer[0].
Chris Freeman
Treehouse Moderator 68,468 PointsWhile the space after answer shouldn't be there, strangely, it does not affect the performance.
$ python3
Python 3.4.0 (default, Apr 11 2014, 13:05:11)
[GCC 4.8.2] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> answer = (False, "alice")
>>> if answer:
... print( "True, because 'answer' exists" )
... else:
... print( "False" )
...
True, because 'answer' exists
>>> if answer[0]: # without space
... print( "True" )
... else:
... print( "False" )
...
False
>>> if answer [0]: # with space
... print( "True" )
... else:
... print( "False" )
...
False
>>> if answer[1]: # without space
... print( "True" )
... else:
... print( "False" )
...
True
>>> if answer [1]: # with space
... print( "True" )
... else:
... print( "False" )
...
True
>>>
The interpreter is quite forgiving when it comes to spaces:
>>> answer
(False, 'alice')
>>> answer [0]
False
>>> answer [1]
'alice'
>>>
Kenneth Love
Treehouse Guest Teacherchris freeman Yeah, it shouldn't affect anything because Python can figure it out. But small syntax things like that often lead to bigger problems. Good to avoid them if possible!
Chris Freeman
Treehouse Moderator 68,468 PointsKenneth Love I completely agree with removing the extra space there. As for OP, their this is not the source of their issue. As written, the code "works".
2 Answers
Chris Freeman
Treehouse Moderator 68,468 PointsYour code works for me. Though at first I was getting 9 of 10 correct (or less) for a while. Turns out I wasn't reading the questions correctly and mistaking "X" for "+", etc. I added a print line in Quiz.ask() to show the "truth":
# check the answer
print( "expect:{}, got:{}".format(answer, question.answer))
if answer == str(question.answer):
correct = True
After adding this I could clearly see that the issue was with my answers and not your code.
Nice work.
Joshua Armstrong
4,921 PointsThanks everybody. Kenneth Love Thanks for responding. I'm actually very honored to have a question acknowledged by you. And everybody else I appreciate your answers! Thanks for the help
Joshua Armstrong
4,921 PointsJoshua Armstrong
4,921 PointsIf I get all of the answers correct it will come back as 3 of 10 or 7 of 10.