Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Preview
Start a free Courses trial
to watch this video
Let's finish the last few pieces of our app and test it out.
Okay.
0:00
So we got this working like we want it to.
0:01
So that's it for the internet.
0:03
We still need to handle taking the quiz
and stuff.
0:04
So let's get out of that, and
0:07
let's go up here and we've got take quiz
and ask that we still need to write.
0:11
So, let's handle the asking part here.
0:16
Take out that pass.
0:21
Okay, so log the start time.
0:22
I think I actually wanna hold on to
something else first,
0:25
I wanna hold on to correct, and I'm gonna
make that equal to false.
0:27
So this says that, by default, they got
the question wrong.
0:31
Okay, so we log the start time question
start equals datetime.datetime.now.
0:35
And then answer would be input,
0:42
and we're going to do question.text plus
space and equal sign and a space.
0:47
We could this with a format string if we
wanted to, but
0:54
I wanted to bring back that plus sign
because we haven't done that in a while.
0:56
If answer is equal to the string version
of the questions answer.
1:00
And then we're going to set correct equal
to true.
1:06
Okay, now no matter what, we're going to
log the end time, so
1:11
question end would be
datetime.datetime.now.
1:14
And then what we want to do is we want to
send back whether or
1:19
not they got the question correct and
question end minus question start.
1:23
So the elapsed time between one and the
next.
1:29
Or sorry.
Between the end and and
1:33
the beginning how much, how much time it
took.
1:35
So, that's just ask, that's just the part
where we ask the the questions.
1:37
We need to actually handle taking the
quiz.
1:41
So let's go up here and figure out how to
take the quiz.
1:43
So, here's what we're going to do.
1:47
Cuz we need to figure this out, right.
1:50
So first of all we want to log the start
time.
1:51
So let's do self.starttime equals
datetime.datetime.now.
1:53
Now, you remember that down here in our
summary we're using
1:59
self.starttime self.end time so we need to
remember to hold on to this.
2:03
Okay, so that's going to be right here.
2:10
So, now, we need to ask all of the
questions.
2:12
So for question in self.questions, we want
to loop through that.
2:15
Self.answers.append.
2:23
So we're going to stick into answers.
2:26
What gets returned when we call self.ask
for the question that we're currently on.
2:28
And then,
2:34
we're going to bring in something new here
which is this else keyword.
2:35
So, the for loop runs.
2:39
As long as nothing inside the for loop
breaks, or throws an error to where
2:41
the loop ends prematurely, as long as the
loop gets all the way to the end.
2:45
Then the else happens.
2:50
It's a little funnily named.
2:51
It ought to be like vin instead of else.
2:53
But that's what it is.
2:55
So we wanna do self.end time equals
daytime.daytime.now.
2:57
And then, at the bottom, here, which
really, we can do.
3:03
That one goes there.
3:10
That one goes there.
3:13
And then, here, we want to return
self.summary.
3:16
So, the else is the only thing new here.
3:19
We talked about that.
3:21
That's, that's what fires, so long as the
for loop completes correctly.
3:22
I still think it's a funny name, but
that's what it is.
3:26
We need to do one more thing before we run
this.
3:29
And what I want to is, down here at the
very bottom, all the way at the bottom,
3:31
and completely outside of the class.
3:36
I want to call quiz and then call take
quiz.
3:40
So that way we can just run the file, and
it will start the quiz up for us.
3:46
Okay, so let's give that a try.
3:50
Come down here and we can do python
quiz.py.
3:52
3 plus 10, 13.
3:58
6 plus 9, 15.
4:00
45, 13,
4:01
9, 9.
4:09
And I got 10 out of 10 right and it took
16 seconds.
4:15
So awesome, that worked just like we
wanted it to.
4:18
That quiz app is simple,
4:22
but uses quite a bit of the Python that
we've learned so far.
4:24
You can take it farther too.
4:27
Change the app so it takes a number of
questions to generate.
4:29
Make it so that a user can specify the
range they want the numbers to come from.
4:32
Or add in some further stats that tell you
how long it took to answer each question.
4:36
In our next stage we are going to cover a
common developer nightmare.
4:41
I can't promise you pleasant dreams, but
4:44
I'll do my best to shine a light on the
monsters under the bed.
4:45
You need to sign up for Treehouse in order to download course files.
Sign up