 Welcome to code.org app lab simple personality quiz summer versus winter with me Tokyo EdTech So today what we're going to do is we're going to take a look at how to make a simple personality quiz Now if you haven't seen my Simple quiz app video you probably want to watch that first or if you haven't walked through the code.org app lab Tutorial you probably want to do that first. So what I want to focus on today is the coding So I'm going to show you What the app looks like I've already gone through the design phase so you can see here When we start the app, we have our main screen and a little bit of a label title And it says are you a summer or a winter person and we have a start button now That's obviously there's not I didn't take a lot of time to design this Just trying to show the basic concepts. So my quiz has two questions So I have a screen for question one a screen for question two have a screen for the results so my question one is do you like ice cream or hot tea and Question two is do you like spicy food or not spicy food? And then based on that we will give you a result. So I'm going to show you how to do this sort of thing now this could be used as I know some of my students are doing which friends character are you or which You know Harry Potter house are you? So you can use this basic concept to do all kinds of quizzes like that So let's go ahead and take a look at the coding. So I'm going to go to So assuming the design part is done and assuming that you've labeled everything You know basically so that we know what everything is everything has a unique name and it is Logical so I go back to the code part. So our very first screen is screen main So there is a start button and when I click the start button, I want to go to screen question one So pretty straightforward. So I'm gonna go to on event and again, I explain this in my other video the Simple quiz video or you again, they have a very good walk through a tutorial here. So when I click on button Start I'm going to set the screen To screen question one now Notice how easy this is to read because I've used good naming conventions. So I'm gonna run it and just test it So I click start and it takes me to the first screen. So I'm gonna hit reset now This is where you have to think a little bit about how this is gonna function now in the other video where it was Just a score either you had the question answers right or you had them wrong We just kept track of the score, but here we need to keep track of whether you are a summer person or a winter person So what I'm gonna do is I'm gonna go to variables And I'm gonna scroll up and you'll see there x equals So what I do is I'm gonna say summer Equals Zero make sure you have the bear there and I'm gonna go ahead and do the same thing and the bear winter equals Zero So when I start my program, I don't know am I summer person or am I a winter person? So I'm gonna go to my first question screen. You'll see ice cream You'll see hot tea So if you are ice cream So what I'm saying is ice cream makes you a summer person Guys you like to be cool and hot tea makes you a winter person because that's what you drink in the winter At least here in Japan. That's what we drink So I'm gonna go ahead and do again on event So if I click the ice cream button I'm gonna do the following. I'm gonna say that I Am a summer person So my summer equals summer plus one. So I'm adding one to summer Now once I've selected that I should go to screen question two So I'm gonna go ahead and add that part in there as well. So I'm gonna go back to UI controls I'm gonna scroll down to set screen And I'm gonna go to screen question two Now I'm gonna do the same thing for hot tea I'm gonna scroll up to on event. I'm gonna do button hot tea and In this case, it's gonna be I think that that means you're a winter person. I'm gonna say winter equals winter Plus one Hey, that's when I click out of there. It you know fixes the blocks Now I also got to do the same thing. I got to set the screen to Screen question two. Let's go ahead and run it and this is always a good idea to test it So ice cream takes me to the next screen I test the oops run it and test it again hot tea takes me to the right screen And again, I'm testing it as I go along because it's a lot easier to fix a problem if I know where I just added the code So if I had a problem there, I know the problem is here Beginners, I don't know why I have this tendency to just try and code everything and then go back and test it And they don't know what they're doing. Okay, so this is my advice to you. So anyway, so let's go to question two So says do you like spicy food or not spicy food? So again in my case, I'm gonna say spicy food means you are a winter person and Not spicy food means you are a summer person. Now. This is just my opinion. You can choose what you want So I'm gonna go ahead to on event Drag that down here. And we're gonna say button spicy Means that you are a I'm sure I just said was that summer or winter, but it doesn't matter. We just kind of play around with it So I'm gonna say if you like spicy food You are I guess we'll keep it consistent. We'll say you are a winter person And winter equals winter plus one And again, we're gonna go ahead and set the screen to result There's a little extra code. We're gonna have to do it here, but you'll see that in a minute Then again, we're gonna go ahead and pull out on events again And if you click button Not spicy We're gonna say that you are a summer person and you can argue that it could go either way We're just trying to make it make a point here And we'll go ahead and set screen again to screen results Alrighty, let's go ahead and run it and test it. So start ice cream spicy You are a blank person because that's what my Result screen looked like let's run it again start when I test hot tea not spicy and it takes me to you are a blank person I'm gonna reset that now. Now. This is where it gets a little bit Maybe look not complicated like we could do it a couple different ways, but what I want to do is I want to do it as Simply as I can So what I want to do is I want to create a function Okay, so a function is just like a piece of code that kind of does something it doesn't it's its own little section So watch what I do here. I'm gonna create a function and I'm gonna call it Set result Okay, because what we want to do is we want to set the result now watch what I do here This is a little complicated if you haven't coded before I'm gonna say a very X equals Max That's gonna be summer And I want to say there result Equals quote summer end quote Okay, so what I'm saying here is that the maximum value is Equal to the number of summers that we chose so that could be one it could be zero it could be one could be two and The result is summer. We're gonna assume that the person chose summer and this is where it's a little confusing Then I'm gonna do the following. I'm gonna do if So I'm gonna say Now we've done summer so if winter is greater than max And I hit click outside that you'll see this happen So if winter is greater than max the maximum number That means that max or sorry winter is greater than summer a little confusing but Trust me on this one So that means that winter is the winner. So what I'll do is I'll say And notice there's no there here. I'll say max Equals winter and I'll say result Equals winter notice there's quotation marks here There's no quotation marks here now what I'm gonna do is the following on My screen result you'll see it says label result LBL result. I'm gonna go ahead and do it a UI controls I'm gonna set property Notice where that's at. This is after the blue section label result the text equals You are a I'm gonna put plus result plus quote space Person, and then you can't see it. It's on the screen Let me see if I can I'll make this a little smaller just for now So you see label result text equals you are a plus result notice the quotation marks Here and here but not for result because result is a variable and This is where it comes from So when I click button spicy because it's the last screen before the result I Need to do the following. I need to actually call the function So I'm gonna say set result And we go ahead and do the same thing You might have to like pause this and watch it So set result So that is the code Okay, so let me explain this one more time and I'll show you how it works in a second Let me explain it. So we start our program summer winter zero Press the start button it goes to From here to screen one If I'm on screen one And I click the button ice cream that means you're a summer person We add one to summer and then we go to screen two if you click button hot tea It means you're a winter person we add one to winter and then we go to the second screen We're on the second screen. You asked do you like spicy food? and if you click spicy food Winter equals you add one to winter. We're saying spicy people like winter and Because it's the last question. We did this function called set result, which I'll explain in a second And then we go to the screen result Then if you're not spicy means you're a summer person and We again set the result. I'll talk about that in a second and we set the screen to screen result Now set result is very interesting We assume that the maximum value is summer. So that could be zero one or two And that means our result is summer However, if winter is greater than max So that means that there are more winter choices Max is winter and result is winter and then we just set the property of label result to The text to you are a blank result, which could be winter or summer person And that's that now if you have something like let's say you're doing the houses for Harry Potter You've got Gryffindor. You've got Slytherin. I forget what the other ones are Ravenclaw and I can't remember the last one You'd have one This is your default and you have one two three if statements and then this will be your last statement Let's go ahead and run it and try it So let's hit start so ice cream makes means we are a think summer person spicy means we are a summer person Says you're a summer person. Yay. Let's try it again Let's go ahead and hit hot tea Not spicy Oops hot tea not spicy. Let's try it again. Oh Hot tea not spicy summer. Okay, okay, we'll do hot tea Spicy and you are a winter person now. You can probably see that. Okay, if there's a tie We might have a problem and it just kind of goes with the default Depending on how you do your numbers and how many different questions you have you may or may not have ties If you have an odd number of questions, it's unlikely to have a tie. I think it's mathematically impossible But you kind of get the idea hopefully of how this works So if you're having trouble understanding it go back watch the video again, you know Pause look at the code. It's pretty. I hopefully Straightforward. Okay, so you've got your screens So a main screen result screen and a screen for each question each Question screen has some choices each choice relates to one of the results and then that's it That's pretty straightforward. So hopefully that was a little fast but hopefully you can go back and Catch what you need to catch from that. So anyway, thanks for watching. Keep on coding. Take care