3.3 Use conditional "if" statements - Video Tutorials & Practice Problems
Video duration:
13m
Play a video:
Video transcript
<v ->So, now that we've learned about comparisons</v> and producing true or false statements, we can start looking at conditionals. Conditionals are like being able to, you can now create a flow chart with your code, and you can get to a diamond and ask a question, and if the questions is true or yes, then go along one path. And if it's false or no, go along a different path. Okay, so for this I'm gonna go back into scratch.py. That's because we're gonna be writing some multi-line code and I don't like doing that in the console. It's a little, it's harder to get all the formatting right. Okay, so the first thing we're gonna look at is just using an if statement. I can create a variable called hungry and just give it a Boolean value of true. And now I can say, if hungry: and then enter and you can see that just like when we defined functions, it's tabbed in four spaces. One, two, three, four. And then, it's expecting something to be in here that's why there's a little red underline. So, if hungry then print go eat something. And then, I can delete the next line and go back and remove the indentation. And then, print continue with your day. So, just like in functions, anything that's indented after this line with a colon at the end, it'll, that belongs, this indentation code, belongs to the previous line and anything that's not indented, that level, doesn't. So, let's see, we'll run this. And because hungry is true, this resulted true, and then it'll print this. It'll go here and then afterwards it'll do this. So, both of those are printed out. But, if hungry is false, I can run it again. And it checks this, it's false, so it doesn't do this stuff and it skips over it. And then, just prints this. So, I will note again that everything under, everything under this if should be indented to the same amount. So... If I had this code, this is all fine, but if these are different, say there's an extra space. Now, it's confused. Or I say one less space, that also won't work. So, this is one of the common errors I see with people who are new to Python programming. And just use the editor as your guide of whether something is wrong or not. You know, if I had an extra quote in here, then this text would change and you've got all these underlines. And it's telling you it's not gonna run. And it doesn't. So, in this case if we wanted to actually have that in, I could escape that character. And now, everything looks good to go. Kay, so that's for if you just have one if statement, but we can also say else... You know, maybe I'm... Maybe I really want you to eat cause it's dinner time. (laughs) I'm gonna say eat anyways. So, I'm gonna remove this cause that doesn't make sense anymore with this silly example. And now, it says, it does the same thing. Go eat something and then, continue with your day. But then, if this is false... It'll say eat anyways. It will ignore this, skip over to this else, and do this if this is not true. And then, print this at the end. So, lastly we can actually have more than just one or two cases. We can have any number of cases by using elif which is short for else if. And elif also needs to have some sort of comparison in it. If it didn't, then there would be no, like else can't have a comparison in it and elif does need to. Cause it's an if statement. So, I'm gonna actually change this. Water temp, and I'll give it a number. Kay, so I can say if water temp is less than zero, print it's freezing, elif water temp is greater than 100, I can print it's boiling. And else, print it's drinkable or you know, maybe it's not drinkable, maybe it's just liquid. Kay, so I can run this and it's equal to zero, not less than zero, I'm actually not really sure what happens when it's exactly at zero. I guess it's freezing, but it'll take a really long time to freeze. So, maybe let's add another situation there. That where water temp is equal to zero and we can say it's slushy or something. Okay, so now it gets in here. So, what this is gonna do, you can have as many elif statements as you want, zero or a hundred or more and it's going to just evaluate the first one, if that's true, just do it. If it's false, it'll look at the next one and if that's true, do it. If that's false, it'll go to the next one and if none of them are true, it'll go to else. So, we'll just make sure that it's working. And, great. So, if we, we don't have to have this else in here. And that's fine, it just won't do anything. It won't print anything because there's nothing afterwards. And, but we do have to have an if statement if we have an elif or else. We can also, yeah. You can have as many of these if you want. If this was an if, it would actually check both of these. So, if this was true, then, it would just go, it would still check this because else if, if there's an else, it just means if the first one wasn't true. Okay, so now we're gonna work on problem number six. And it'll just be a simple guess a number between one and 10. You have one shot and if you get it right, it'll say so. And if you get it wrong, whether you should guess higher or lower. So, let's open up problem six, Lucky Number Guess up high. And I have some code in here already and we've got some creating a random number between one and ten. And then, accepting input for a user's guess and casting it to an integer. So, once that's done, we want to check for different cases. And then, print what the final number was. So, if the number is correct, so that would be if, space, answer, oh maybe I'll do if the guess is equal to the answer, and remember that we need two equals, then we can print correct. And then otherwise, tell them if the if the answer if higher, lower. So, we could do an else and then, do another if inside of here. If guess is less than answer, print nope, it's higher. And then, else, print nope, it's lower. So, this could be an elif and guess is greater than answer, but since we've already gotten the, checked for the condition where guess is equal to answer, then there's only two other cases, so we can just ignore one of these and do else. And then, let's run this. So, I guessed one. It's higher, the number was 10. That seems good, I guess 10. Nope it's lower, the number was one. Oh, that's weird. And then, that was close. If I wanted to check if the answer is correct, then I can just replace this for now. With five. Run it again. Oops. Replace this one. Five, and then correct. The number was five. So, I'll undo that and then, also... I can actually get rid of this else and have this as elif instead and just break this problem down into three cases which reduces it's complexity. So, now that I've done that, my formatting is all off. If I run it, doesn't match any indentation level, so I would move that, and move that, and move that. And now, we've just got two levels of indentation. Great. And we can remove these too. So, in case you're wondering, I'm pressing Command + K to delete the entire line. And that's one of the PyCharm keyboard shortcuts that I use frequently. So, now that we have gotten this far in the problem, we're ready to work on the next challenge which is making a larger guessing game that allows you to have more tries.