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 trialAizah Sadiq
2,435 PointsThis is not working
When I run it I get this
treehouse:~/workspace$ python
Python 3.6.4 (default, Nov 19 2019, 17:12:46)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from quiz import Quiz
>>> quiz1 = Quiz()
>>> quiz1.answers
[]
>>> quiz1.questions
[<class 'questions.Multiply'>, <class 'questions.Add'>, <class 'questions.Add'>, <class
'questions.Add'>, <class 'questions.Multiply'>, <class 'questions.Add'>, <class 'questio
ns.Multiply'>, <class 'questions.Add'>, <class 'questions.Multiply'>, <class 'questions.
Multiply'>]
>>> quiz1.questions[0].text
>>>
As you can see it gives back nothing. Here is my code
import datetime
import random
from questions import Add, Multiply
class Quiz:
questions = []
answers = []
def __init__(self):
question_types = [Add, Multiply]
for _ in range(10):
num1 = random.randint(1, 10)
num2 = random.randint(1, 10)
question = random.choice(question_types)
self.questions.append(question)
def take_quiz(self):
# log the start time
pass
# ask all of the questions
# log if they got the question right
# log the end time
# show a summary
def total_correct(self):
total = 0
for answer in self.answers:
if answer[0] == True:
total += 1
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
def summary(self):
print(f"You got {self.total_correct()}/{self.questions}")
print(f"It took you a total of {(self.end_time.start_time)} seconds")
4 Answers
Vanessa Schwegman
3,587 PointsYou aren't sending any arguments to your randomly selected Add and Multiply classes in the following line:
question = random.choice(question_types)
Depending on which class stored in question_types is randomly selected during the loop's iteration, you are currently creating the question variable as one of the following: question = Add question = Multipy
Both of these classes are missing two arguments. It looks like you may be assigning the class itself to the question variable instead of creating an instance of the class to assign to question.
You should pass two numbers in order to instantiate the class. In the context of this for loop, pass the two random numbers between 1 and 10 assigned earlier in the loop to num1 and num2:
question = random.choice(question_types)(num1, num2)
Depending on which class stored in question_types is randomly selected and depending on which two random numbers between 1 and 10 are selected for num1 and num2, your assignment to the question variable might look something like the following during one of the loop's iterations:
question = Add(5, 7)
Josh Keenan
20,315 PointsYou don't need any .text
at the end.
Aizah Sadiq
2,435 PointsWhen I did that this happened
treehouse:~/workspace$ python
Python 3.6.4 (default, Nov 19 2019, 17:12:46)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from quiz import Quiz
>>> quiz1 = Quiz()
>>> quiz1.answers
[]
>>> quiz1.questions
[<class 'questions.Multiply'>, <class 'questions.Add'>, <class 'questions.Multiply'>, <class 'que
stions.Multiply'>, <class 'questions.Add'>, <class 'questions.Add'>, <class 'questions.Multiply'>
, <class 'questions.Multiply'>, <class 'questions.Add'>, <class 'questions.Multiply'>]
>>> quiz1.questions[0]
<class 'questions.Multiply'>
Josh Keenan
20,315 PointsPrint it out, if it doesn't come out how you want you should write the __str__
for your class so it is output correctly.
Aizah Sadiq
2,435 PointsIt's still not working