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
Danica Patrick Eats a Scorpion | Running Wild with Bear Grylls
BEAR: Let’s make a little nature’s candle out of rocks. DANICA: Get some rocks? - Yeah. DANICA: OK. BEAR (VOICEOVER): Danica Patrick and I are in the heart of the vast Utah desert. She doesn’t know it yet, but Danica just found us something to eat. Oh!…
Bill Ackman: The Biggest Investing Opportunity of Your Life
I think you can do very well as a stock market investor if you find really high quality companies, and you buy them at attractive prices. I think today, you know, is a pretty good time. It’s a pretty good point of entry. Now, it’s no secret that this year…
For Syrian Refugees, He Is a Friendly Face in a Strange New Land | Short Film Showcase
I think that facing death changes people, which is what happened with me. Before this experience, I was a completely different person with a completely different dream. My last dream, which was to treat cancer, and right now my dream of changing the world…
Exploring Ciudad Perdida | Lost Cities With Albert Lin
[music playing] ALBERT LIN: It’s literally a city in the clouds. Maybe those Spanish stories weren’t just legends because that’s what a real lost city looks like. HELICOPTER PILOT: [inaudible] 1 0 1 2. ALBERT LIN: That’s Ciudad Perdida, the Lost City. …
Behind the Scenes: Documenting the Elusive Florida Panther | National Geographic
Foreign and that’s how you test. I don’t think I had any idea what I was getting into at the beginning of this project. I’ve only seen a Florida panther twice with my own eyes. The animals that we’re trying to film and photograph are super elusive. There’…
The Mani Tribe's Blowgun | Primal Survivor
[music playing] HAZEN AUDEL: The Mani have an unrivaled knowledge of the local plants and trees, relying on them for almost everything they need. [non-english speech] Huh? [non-english speech] [non-english speech] That one right there. [non-english speec…