if if if if if….elif

Video Notes:

What I use to learn (the BEST IT training): https://ntck.co/itprotv (30% off FOREVER) *affiliate link

In this episode of Learn Python RIGHT NOW, NetworkChuck teaches how to let good Ben in using Nested Ifs and Elif statements!

FREE Python Lab: https://ntck.co/pythonep5
Support the course: https://ntck.co/pythonrightnow

0:00 ⏩ Intro
2:50 ⏩ Figure out if Ben is EVIL
3:23 ⏩ STEP ONE: Ask Ben if he is evil
4:22 ⏩ NEW CONCEPT: Nested IFs!!
5:48 ⏩ Nest the code under the Second If statement
6:38 ⏩ No nice Greeting for Nice Ben?!
7:18 ⏩ Make Ben feel special!!
9:50 ⏩ Robot Barista is doing a great job but changes are needed
11:03 ⏩ Messy fix for the menu
12:46 ⏩ NEW CONCEPT: Else If (ELIF)
14:58 ⏩ Let’s add an else
16:25 ⏩ CHALLENGE: Whipped Cream??
17:04 ⏩ Outro

If EIF Else, if, if else you got it. Okay. Let’s get started.
Welcome to episode five in my Python right now, series where I’m gonna teach you everything you pretty much need to know to become dangerous and Python. Now, if you don’t know what I was talking about in the intro print that’s okay. But go back and watch the previous episode else. Let’s continue. Because in this episode, we’re gonna build on our flow control or controlling the flow of our Python programming. If something is true, then do something. And in the episode, you helped me prevent evil bin from entering our coffee shop. We said, if evil bin comes in, kick a mapped else, Hey, you’re good to go continue. But now we gotta build on that because I figured out not all bins are evil, who would’ve thought. So there are some good bins out there. So we now have to put into our Python programming a way to flesh out the evil bins and let the good ones in.
But to do that, we have to introduce some new concepts of how we control the flow in our Python programming, who this one’s gonna be fun. So get ready, get your coffee set up because that’s pretty much all you need in this because I’m providing a free Python lab right here in your browser. Check it out. Link below, get signed up because the best way to learn Python is to actually just do it to lab it up. And you’re gonna do that right now, here with me. Oh, by the way, this entire series is free. Thanks to our sponsor. It pro TV. Now, if you don’t know what it pro TV is print, you’re crazy because they are the best it training platform out there. They’re what I use to keep my skills up to date. And that’s why I love them because they have things for people at every level in their it journey.
So for me, I’ve been doing things for a while, so they have perfect things to help me upskill my, I don’t know why I said that weird. And also if you’re just like getting started starting at zero, they have everything for you from getting started with your a plus certification to network, plus to jumping up to your CCNA and then maybe becoming a hacker and they have hacking courses, they got it all. And not only that, you’re not just gonna be sitting there watching a video. They have labs, the things they teach you, you can implement immediately start playing with it right away. So if you love it and you wanna earn like me to not mean that’s a rhyme, check it out, link below. And if you use code network, Chuck, you get 30% off forever. So check him out. Okay. Quick coffee break.
Before we get started, gotta fill up just a little bit. Ah, network chuck.coffee. Now first let’s go ahead and get our Python lab environment fired up. So again, check that link of the description. Sign up for my free course and launch that first flab for episode five. Last episode, we did a great job. Keeping evil Ben out of the coffee shop with yo bro, what’s your name? And if he said, Ben, dude, we printed. You’re not welcome here. Kick him out and exit. Let’s test it out real quick. Let’s go ahead and run this code, click it on a run right here at the top run. What’s your name? Let’s just say we’re Ben today. We’re Ben feeling kind of evil. Whoa, immediate. Now I have to apologize to you Ben, because not all Bens are evil and we have to reflect that in our script right now we gotta figure out, Hey, is Ben evil and what’s the best way to find out if a Ben is evil, it’s simple, right?
You just, you just ask him. So let’s ask Ben an evil person will be honest. So here’s our first task. If their name is Ben, we need to ask them, Hey, are you evil? If they answer yes, kick ’em out. If they answer no we’ll then duh, they’re not evil. And they can continue on real quick. Before I show you how to do this. And this will be a new concept. I wanna have you try it. This will help you kind of understand the logic of we’re about to do so pause and try to do this
On pause. Let me walk you through it real quick. So the first task in this task is we have to ask Ben, are you evil? But we only need to ask Ben, like, if we put the question up here and like Brittany comes in, we’re like, what’s your name, Brittany, are you evil, Brittany? That wouldn’t make any sense. Cuz only Ben’s are evil. So we have to accommodate for that logic. So we need to ask only bends if they’re evil. So the question’s gonna have to go over here in our if statement. So that’s where I’ll put it. I’ll put it right on top of this first print statement. So I’ll get in there. I’ll put my cursor at the end of the, if statement hit enter and I’ll create a new variable, I’ll say evil underscore status and I’ll have that equal. And this will be an input function.
Cause I’m gonna be asking Ben a question input and I’ll say the string or you and evil question mark. And I’ll do a back slash end to have a nice little space. So cool. We’re now finding out if Ben is evil and this evil status will either answer or equal to yes or no. And we have to do something with that information. If it’s yes, we have to kick him out. If it’s no, we have to let him come in. It sounds like we might need another if statement, right? If you thought that you’re exactly right. New concept time, we’re talking about something called nested ifs. We’re gonna build a nest of ifs here and check it out. Here’s what we’ll do. We’ll go and just do this right now. So just after our evil status equals, are you evil? I’m gonna enter and I’m gonna create another if statement and notice it’s nested, it’s underneath this other.
If Python’s getting crazy, man, here we go. If and now we’re, we’re going to evaluate the evil status. If evil status equals are double equals and we’ll say the string, yes. Then we’ll put our colon. If that evaluates the true, then we’re doing something. Look at this. We got two ifs. If, if, if that’s what’s happening here now here’s the power of nested ifs. First we’re saying if the name is Ben than do something. And when we nest that next, if under that, we’re saying also, Hey, if his name is Ben and he’s evil. If the evil status is yes, then do something. If both of those things are true and remember, keep an eye on that. Spacing the indentation. The reason we know this, if statement is nested under that, if is because it’s indented under it now, knowing how I and spacing is, let me ask you a question.
Will this print statement run? If Ben is evil, the answer is yes, because right now that code is nested under the initial. If statement and that all that’s evaluating for is if your name is Ben. So regardless of your evil status, if your name is Ben, it’s gonna run that code. So now we have to make sure that we only run the code. If your name is Ben and your evil, how do we do that? Well with understanding how important spacing is a Python. Again, I’m gonna mention that a lot. We need to nest that code underneath the second. If statement that way it’ll only run. If your name is Ben and you are indeed evil. So over here on our if statement or I mean our print statement right here, just gonna backspace that, then I’m gonna tab it over to be nested right there and also for the exit as well.
It’s gonna tab that underneath. So now, now it’s gonna work. So at this point, this code right here will only run if their name is Ben and Ben is evil. If his name is just Ben, it’ll move on right along. Let’s try it out real quick. Let’s go ahead and run our code run. What’s your name? We are still evil. Ben today. Evil Ben or just Ben, are you evil? Let’s say yes. We’re not welcome here. Let’s try it again. Click on run this time. We won’t be evil. We we’ll still be Ben, but we’ll be one of those rare Bens that aren’t evil. Are you evil? No. Now cool. Something just happened there. First. We didn’t get kicked out, but we also didn’t get a nice, like friendly greeting, like the robot baristas program to do. We didn’t get this. Why do you know this else?
Statement will only run if that isn’t true. Cuz it’s in line with this. If statement again, spacing an indentation is so important at Python. It might be one of your bigger headaches, but once you learn it and get used to it, it’ll be your best friend. So that else is tied to this. If statement based on the spacing. So if your name is Ben cool, we’re gonna run everything under here. If your name isn’t Ben, we’re gonna run this. So now we need to add some code to make Ben feel a little special cuz Hey, non evil Bens are pretty special. So let’s, let’s do something real quick. Actually. Let’s see if you can do it. How can we make Ben feel special? How can we make the non Bens feel welcome? So do something similar here. I don’t care what you’re right, but make Ben feel good. Pause the video and try it.
UN pause. Let’s try it out real quick. Here’s what I’m gonna try. Just like we can nest our ifs. We can also nest our ELs. So here we go. Who, who Python gets crazy. I’m gonna put my cursor right over next to my exit. Hit enter. And then I’ll do a shift tab to back out to where I’m in line with that nested. If and here is where I’ll type in my else. Else. Colon hit enter and here is where I’ll make Ben feel welcome print. Oh, so you’re one of those good Ben come on in. So bam, just right here. Who we’ve got an entire, if L statement thing going on, that’s nested inside this if statement and all this code right here will only run if their name is Ben. Not enough talking about it. Let’s run our code. I’m so excited. Coffee break, click on run.
And we’ll be Ben. This time run. What’s your name? I’m still Ben. Am I evil? Let’s say no, no I’m good. Ben. Today it worked. So you’re one of those good Bens. Come on in. Let’s be evil again. Let’s make sure our code works. Test it thoroughly. What’s your name? My name is Ben. Are you evil? Yes, I got kicked out. Oh my gosh. And let’s test it one more time. Let’s make sure that um, Brittany can still come in Brittany. Okay. And she just comes up on through cuz her name’s not Ben, obviously. She’s okay now isn’t that just awesome. And let me tell you, you can do a ton of ifs. Check it out. I can do, if two is less than three, then do something. I can even nest an if under that nested. If so, if one is greater than four than do something, I could continually nest as many ifs as my heart desires and go crazy.
Now you shouldn’t do that because that’s insane. But try it real quick. See how many, if you can nest, how many things you can test out go. The cool part is that that works, but it gets messy and confusing and you would never wanna do that. Also keeping in mind that at the very bottom of your, if craziness on the last little if statement, it’s saying, if this is true and this is true and this is true, then this will happen. All of those have to be true. And that’s where nested ifs really come in handy is if you wanna make sure that multiple things are true at the the same time, but now there’s something else that’s even more powerful. Uh, probably equally powerful. It’s all awesome. And it’s gonna help us with our menu. You see right now, robot barista, he’s doing a good job and tell you what go and open up the second lab for this episode, episode five.
And we’ll get going here in episode four. And in the bonus lab we had after it, we made some changes to our menu. We took a big leap in and we added frappuccinos to our menu. So we added it here. And the big kicker was that frappuccinos cost more money. They’re just more expensive to make. So as it was, we charged $8 for whatever someone ordered a black coffee latte, cappuccino, all eight bucks, frappuccinos cost us more. So we had to charge more for it. So we had to find a way to make our price be different. If they, he ordered a frappuccino, we did that with a beautiful, just a beautiful, wow, chef’s kiss. If statement, sorry for that. Um, and we did that right down here. We said, Hey, if the customer ordered a frappuccino, dude, that price is gonna be $13. Cuz that’s, that’s some bougie stuff else.
If it’s regular stuff like we have on the menu if eight bucks, but now we have a new problem. You see customers complaining that our prices should not be the same for all coffees. Black coffee by itself should be less than $8. So shouldn’t espresso a lot. They should be a bit more and a cappuccino should be a bit more. We need to make sure that our menu items have different prices. Now this will be a fun one and I’m going to introduce a new concept. But before I do that, I want you to try this with just if statements it is possible and it’s gonna be a bit messy, but I want you to try it, pause the video, get the logic behind it and go,
All right, UN pause. Now here’s how you would do it. The messy way. We could simply make a bunch of these, a bunch of these. If statements like this, watch just take that copy, paste that here and say, instead of Rapino gimme black coffee and we’ll make that price $3. That sounds reasonable. Right? You would get some black coffee for three bucks. Yeah, come down and we’ll do it again. Espresso latte and then cappuccino and tell you what let’s go ahead and remove our else’s cuz this will mess things up. So remove each else and also think about why that would mess things up. I’ll explain it here a bit. So we get our, if statements then down here at the bottom of this, I’m going to print the price so we can see what it evaluates to. Let’s go ahead and run our code and let’s order some.
Oh let’s over our lattes. Take eight lattes. Cool. So it print did nine. Is that true? Yes. It totally worked. So that’s one way to do it. We can have it go. Hey, is it a frappuccino set the price to this then it’ll keep going in order. Is it black coffee? If it’s not, it’ll just skip it. If it is. Oh cool. Set the price to this. Now notice these are not nested. If statements, if we had tried that well then that totally would not have worked because if we did a nested statement under like the first one, and we said, if order equals black coffee, well then whenever code runs here, it would only run if the order was black coffee and a frappuccino. And that makes zero sense. Now also earlier I told you that I’d explain this. Um, I had it to where the L statement was gonna set the price to $8.
So let’s say, for example, over here in my latte, if I, uh, add the L statement back in there, price equals eight, let’s run the code and let’s see, I wanted a frappuccino. Well, that’s interesting. The price is eight when it should be 13 because you know what happened? Well, it came in and said, oh yeah dude, it’s totally a frappuccino. We’re gonna set the price to 13. And then it goes through and evaluates each if statement and when it got down here, it goes, is it a latte? No. Well else we’re gonna set it to eight. So that totally wouldn’t work now a different way to do this a better way. This is a new concept. It’s so cool. We’ve got if and we got else. So if that’s true, cool else do something else. But now we also have else. If legit it’s a, it’s a thing else.
If or short it’s EIF, it sounds like I’m making up words. Right. But it’s real and it’s true. And a check it out. It’s awesome. So instead of doing a cajillion individual, if statements let’s try EIF, so I’m gonna step back here and delete all the stuff up to frappuccino. So if order equals frappuccino set the price of 13, just under that, I’m gonna hit it, enter and then spacing, keeping that in mind. Spacing’s important shift tab where I’m equal with, if I’m gonna type in Eiff and then just after EIF, I’ll say order equals black coffee, colon price equals three. Now we’ll just happen to here is so powerful. So first you’re saying, Hey, does the order equal frappuccino? If it does cool, make the price 13 bucks, then it’s like else, but hold on. If else, if it equals black coffee, well then Hey, let’s go ahead and set it to $3.
So in this case, in this example, we’re not nesting anything. Just keep in mind. It’s the spacing’s important. It’s unequal footing with the, uh, if it’s kinda like a variation on else, except we’re evaluating something else. That’s confusing. I know. And we’re not saying that the order has to both equal frappuccino and black coffee. We’re saying, Hey, if it’s frappuccino, cool, do this. If not, Hey, is it black coffee? Cool. Do this. And we can keep going. Let’s try it out. Let’s add another EIF. Cuz you can do that. Hit enter after price. We’ll step back to be equal with EIF. Again, EIF order equals espresso. We’ll do our colon enter and keeping our spacing in mind. We’ll do price equals. What did we say before? Was it five? I think it was $5. And tell you what? Go ahead right now without watching me go ahead and fill in the rest.
Pause the video now. UN pause. I’ll do mine real quick. So now we get all our ifs going on and then we get our E lifts. Let’s run the code. Let’s see what happens. Woohoo run. I would like a, let’s get a latte. I’ll have 10 lattes. It worked. Notice the last number. There is our price. We’re printing the price down here. That’s the last thing our code runs and it matches that number. The logic is working. We’re controlling the flow. Don’t you feel powerful right now. Take a coffee break. Now also just for fun. Let’s at the very end of this entire parade here. Isn’t this much cleaner than a bunch of statements. I think it is anyway at the end of this, let’s do our else and we’ll do print. Sorry we don’t have that here because we’ve included all the things they could order.
If they say anything else, we’ll just say no, we don’t have that. Let’s try it. Let’s run the coat run. I would like a green tea. Oh, come on. Where are you at here? And this logic kind of faulty, but I’ll just put an eight. Oh, you know what? We made a mistake. I, I hope this. I love when, when things like this happen when I’m teaching, cuz it’s like, well, first of all, I’m not perfect far from it. What? It gives you a chance to learn? Like what do we do here? What’s the problem? Well, first I notice the, the big glaring problem. I did you lift price down here, like an idiot. That should be what it should be. Order fixed. No harm done. That’s not the main problem. If I run the code again, I’ll say again, green tea. Uh, we still have a problem.
And it tells us right here in the air name, error price is not defined. We’re trying to print the price down here, but it’s not there. It’s what I can do. I could either just call print price so it doesn’t try to run that code. Or I can just end this L statement, say price equals zero. That way we don’t have a problem anymore. So let’s run the code green tea. I’ll take nine of them. And I says, sorry, we don’t have that here. So in that code, we have our, if your orders, this, do this else. If else, if I crazy else, if, and then finally else, we don’t have that. What are you even doing here? This is a coffee shop now that’s it for the new concepts. But I do have a challenge lab for you. If you wanna try it out.
The link for that and the walkthrough will be in the description below. If you’re watching this on YouTube. So here’s a challenge I want you to do. This will be a fun one. If they order a latte and they want extra whipped cream, this is pretty latte. Do you want whipped cream? Which of course they’re going to want whipped cream. If the answer is yes, make the price $11. If no. Well, keep it at the standard price. Nine bucks. This particular problem will combine everything you’ve learned in this video and previous stuff. So again, check that link below and see what you got. All right. That’s it for Python right now. Episode five, coffee Brook. I told you, I told you I warned you. Python gets crazy. Awesome. And again, that’s gonna get even more crazier and OER. So buckle your seatbelt. Make sure you have coffee on hand.
And also have you hacked the YouTube algorithm today? Let’s make sure you do hit the like button, subscribe, comment, notification belt, all those things because you have to hack YouTube today at the, of course, and also a massive shout out to the sponsor of this entire series. It pro TV. Again, if you wanna get started in it or you want to further your career in it, if you finally wanna get that CCNA or that a plus or that CCMP or your AWS certifications, whatever you wanna do right now is always the time and check ’em out. Link below, use code network check and get 30% off Forever. Yeah, that’s pretty much it I’ll catch you guys next time.

Check Out Network Chuck's Coffee and MERCH Shop