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
What If You Were 620 Miles Long?
Let’s talk about double pain. If your body was 620 mil long, pain could be your alarm clock. You could bite your toe at bedtime and then go to sleep; you wouldn’t feel any pain until the signal from your toe reached your brain and woke you up 8 hours late…
15 Things You Should Know About Your Haters
Fifteen things you should know about your haters. Welcome to A Lux, the place where future billionaires come to get inspired. Hey there, A Luxers! So, we have a juicy video for you today. As you know, success and haters go hand-in-hand. In fact, a good i…
Go Inside an Antarctic 'City' of 400,000 King Penguins — Ep. 4 | Wildlife: Resurrection Island
In one of the world’s largest King penguin colonies, chicks must be able to make it through the winter without food. This chick can expect one of three fates: be eaten alive, starve to death, or hang on one more day until their parents return with food to…
15 Traits Of A Weak Person
We all know a weak person is easily influenced by others’ ideas and opinions, but not necessarily by their own. The confidence that comes from knowing you deserve something motivates you to perform the acts and prove your worth, and you exhibit traits tha…
Example: Graphing y=-cos(π⋅x)+1.5 | Trigonometry | Algebra 2 | Khan Academy
We’re told to graph ( y ) is equal to negative cosine of ( \pi ) times ( x ) plus ( 1.5 ) in the interactive widget, so pause this video and think about how you would do that. And just to explain how this widget works, if you’re trying to do it on Khan A…
Using the Administrator Skills Report to drive differentiation on Khan Academy
Today, I’m going to be showing you how to use the skills progress report in the administrator dashboard and the Khan Academy platform. This is going to be a great tool in order for you to support your teachers in differentiation in their classrooms. You c…