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
Interpret quadratic models: Factored form | Algebra I | Khan Academy
We’re told that Rodrigo watches a helicopter take off from a platform. The height of the helicopter in meters above the ground, t minutes after takeoff, is modeled by… and we see this function right over here. Rodrigo wants to know when the helicopter wil…
National Geographic Takes on New York Fashion Week | Fit for a Queen | NYFW
[Applause] Queens is a project about female leadership, not only in front of the camera but behind the camera, telling a story about nature in a new way. And there couldn’t be a better time in history right now to be getting that message across. The titl…
Solving exponential equations using exponent properties (advanced) | High School Math | Khan Academy
So let’s get even more practice solving some exponential equations. I have two different exponential equations here, and like always, pause the video and see if you can solve for x in both of them. All right, let’s tackle this one in purple first. You mi…
My 2024 Financial Position and Investing Goals Explained.
So I’ve been battling with something over the past year. Part of my content from a year or two ago used to be periodic updates on my own financial goals, my personal finances, and the different ways that I’m trying to make money. Generally, those videos w…
THE END OF $0 REAL ESTATE | Major Changes Explained
What’s up, grandma’s guys? Here. So, a few days ago, I made a video discussing my thoughts on the new personal tax increases along with an analysis of how that would affect the stock market. However, I purposely left out one crucial point, which has the …
Ecosystems and biomes | Ecology and natural systems | High school biology | Khan Academy
So just as a bit of a review, if we take the members of a certain species that share the same area, we call that a population. Population, all of the organisms in this particular population will be members of the same species. There could be other member…