In the last lesson,
we got our quiz app to start displaying the questions that we were getting from
our API. This question I landed on is pretty interesting.
Nintendo started out as a playing card manufacturer?
And I would love to know if that's actually true or false.
So I'd really like these buttons to work basically.
If I click on one of these buttons,
I want to be able to check my answer against the real answer that we got back
from our API. So in this lesson, that's what we're going to get to work.
So I want this to be a bit of a challenge for you.
We know that we can add a command argument to our buttons so that it calls a
particular method when it gets pressed. Now, when that method gets pressed,
we want to go into our quiz brain and call this check answer.
Now this check answer method expects a answer to check against.
Have a think about how you might solve this and get this to work so that we can
start printing out whether if we got the question right or the question
wrong. Pause the video now, and try to complete this challenge.
Go.
Okay. Here's the solution.
The first thing I'm going to do is create a new method called true_pressed
and also a method called a false_pressed.
This true_pressed is obviously going to be the one that will be the command for
this true button and it's going to be self.
true_pressed because we're inside a class and we have to make sure that there's
no parentheses at the end of it because we only want to trigger this method
when the button actually detects a click. In a similar way,
we're going to add a command to the false button and it's going to be
self.false_pressed. And again, deleting the parenthesis.
And now we can figure out what to do in here.
We already have access to our quiz brain object in here
which is saved under self.quiz.
When either of these gets pressed,
then we can simply tap into self.quiz and we can call the check_
answer method that exists over there.
And we can pass over true when true_pressed is called,
and then when false_pressed is called
...
we'll pass in false. Now it's going to go into this check_answer,
pass that answer in and it's going to print out whether if you got it right
or whether if you got it wrong. So let's test that out.
Let's run the code again and make sure that we don't make any silly typos like
comand.
Yeah.
So let's run our app and see if can answer some questions.
Gumbo is a stew that originated in Louisiana.
I think that might be true.
And when I click on the button,
you can see in the console it's printed whether
if I got it right or wrong. Now,
while our code works and we can see whether we got it right or wrong in the
console, that's not something the user will see.
This is only something that's available to us as developers.
So how can we give the user some feedback?
That's what we'll figure out in the next lesson.