yego.me
💡 Stop wasting time. Read Youtube instead of watch. Download Chrome Extension

Checking bus fares with if statements | Intro to CS - Python | Khan Academy


4m read
·Nov 10, 2024

Let's design a program using Boolean expressions and if statements. The public transit system wants to build an app that determines a passenger's bus fare. The standard bus fare is $4.25; however, they offer discounts for certain age groups. Kids under five ride for free, and seniors, those who are 60 and over, get a dollar off their fare.

We want to break down the problem so we can focus on getting one piece working at a time. Let's start with the kids' case first. We want to come up with a Boolean expression that's going to be the condition for our if statement. This Boolean expression should ask the question: does this passenger qualify as a kid for the purposes of the bus fare?

We said a kid must be under five. That means we need some data about the passenger in order to answer this question. We need to know their age, so we'll add an input function call that asks the user to enter their age. Then our Boolean expression would be age less than five.

Let's test that expression real quick... Oh, okay, we got a type error. The error message says the less than operator is not supported between instances of string and integer. Age less than five? I guess it's saying age is a string. Ah, okay, 'cause I didn't cast the result of the input function to an integer, so age is storing the string "3" and not the integer 3.

Okay, let's test that again. That's evaluating to True, which is correct. Now let's try the case where they're over five, and now let's try the case where they're exactly five. Awesome! Okay, double-checking my requirements—it's correct that passengers who are five should not qualify for the kids' fare, so this is working as intended.

We've got a working condition. Let's put it in an if statement, and we make sure to end this with a colon. Now what do we want to happen if the passenger is a kid? Well, we want their bus fare to be free. To do that, inside the if statement, we can set the variable bus_fare to zero. Inside means indented one tab over. Let's check our work there and test both cases, and while we're at it, I'm going to inline this condition into the if statement. It's functionally equivalent, but it looks a bit cleaner, especially with a condition so short.

Now let's do the same for the seniors' case. The condition for a senior is 60 and over, so we'll want our Boolean expression to be age greater than 60. However, this is inclusive, so if you are 60, you are a senior. This should actually be greater than or equal to 60. We'll put that in an if statement. Then if the person is a senior, we want to take a dollar off their fare.

So the instruction we want indented inside the if statement should subtract a dollar from the existing bus fare. Let's test that with both cases where the user is a senior and where the user isn't. Let's also check that our kids' case didn't break.

Now why did I write this assignment statement as bus_fare minus one instead of just setting it directly to 3.25? This way is a bit more future proof. If the transit authority later needs to raise the bus fare by 50 cents, I can just change this first assignment statement to $4.75, and the dollar off senior discount will still work. If I had instead set it to 3.25, I would have to remember to come back here and update this to 3.75 as well.

One less requirement: the transit authority wants the app to tell the passenger which fare they qualified for, in addition to the price. One way to do that could be to just add a print function call inside each of these if statements. I don't love that user experience; I would rather the existing print statement just print out the fare type. So it might say, "The senior bus fare is 3.25." That means we need to remember the fare type so it's still available by the time we get to this print statement at the end.

Sounds like we need a new variable inside the senior if statement. We'll set the fare type to the string "senior," and inside the kids if statement, we'll set the fare type to the string "kids." Then we can concatenate the fare type into our print statement. Okay, now let's go in and check all of the possible cases.

We start with the senior case—looks like that works. Then the kids case—looks like that works. We also want to check the standard case where neither if condition evaluates to True. Whoops! That gives us a name error. The variable fare_type is not defined, which makes sense because the assignment statements to fare_type only exist indented inside the if statement. That means if both conditions evaluate to false, neither of these assignment statements run.

So we need to initialize the variable fare_type so it exists in the standard case. Nice! It works. I'm going to get started on the transit authority's next feature request: deducting bus fares from the user's balance.

More Articles

View All
Scarcity of resources
All of economics is based on this notion of scarcity of resources. What does scarcity mean? Well, in an everyday context, it means that there’s not as much of something, say a resource, as people may need, or there’s not an unlimited amount of something. …
How To Make $100 Per Day With Index Funds
What’s up, Graham? It’s guys here. So there comes a time in everyone’s life where you stop and think to yourself, “How do I make a hundred dollars a day investing in index funds?” Alright, fine, maybe it doesn’t exactly happen like that, but chances are …
Inductor kickback 1 of 2
I want to talk about a new example of an inductor circuit, and we have one shown here where this inductor is now controlled by a switch. This is a push button switch that we move in and out, and this metal plate here will touch these two contacts and comp…
Widowmaker Waves | Wicked Tuna: Outer Banks
The commercial fishing boat, Risky Business, was on his way across the ocean bar. But it was struck by two freak waves. Lion Bridge was nearly ripped away from the hull. That just gave me anxiety! I wish you know, welcome back to the OBX. Yes, they both h…
Finding Fourier coefficients for square wave
So this could very well be an exciting video because we started with this idea of a 4A series that we could take a periodic function and represent it as an infinite sum of weighted cosines and sines. We use that idea to say, well, can we find formulas for…
Showing My Desk to Adam Savage
Hey, Vsauce. Michael here. The eye is a mirror. When you look into an eye, you can see a small, tiny version of yourself that kind of looks like a doll version of yourself. The Latin word for a little doll is “pupilla.” That’s where we get the word “pupil…