Program design: bus fares
About this video
The instructor designs a bus fare program step by step using Boolean expressions and if statements: kids under five ride free and seniors 60 and over get a dollar off the 4.25 fare. Along the way they fix a TypeError by casting input to an integer, test boundary cases, explain why subtracting from the fare is more future proof, and fix a NameError by initializing the fare type variable.
You'll be able to
- break a problem into cases and build one if statement at a time.
- write correct boundary conditions such as age < 5 and age >= 60.
- explain why input must be cast to an integer before comparing it with a number.
- test every possible case and fix a NameError by initializing a variable.
Key words
- TypeError
- An error when an operation is used on the wrong type of data, like comparing a string with an integer.
- cast
- To convert a value from one type to another, such as turning the string "3" into the integer 3 with int().
- inclusive
- Includes the boundary value itself; 60 and over includes 60, so we use >= 60.
- inline
- To put an expression straight into the if statement instead of storing it in a variable first.
- initialize
- To give a variable a starting value before it might be used.
Transcript
- [Instructor] 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 wanna break down the problem so we can focus on getting one piece working at a time. Let's start with the kid's case. First, we wanna 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 < 5.
Let's test that expression real quick. Oof. Okay, we gotta TypeError. The error message says the less than operator is not supported between instances of string and integer. Hmm. 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 three and not the integer three. Okay, let's test that again. That's evaluating two 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 fair. 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 wanna 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 gonna inline this condition into the if statement.
It's functionally equivalent, but it looks a bit cleaner, especially with the 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, so this should actually be greater than or equal to 60. We put that in an if statement, and then if the person is a senior, we wanna 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. And 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.
On this 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 fair 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 fair_type to the string senior and inside the kids' if statement, we'll set the fair_type to the string kids. Then we can concatenate the fair_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. And we also wanna check the standard case where neither if condition evaluates to true. Whoops, that gives us a NameError. The variable fair_type is not defined. Hmm, which makes sense because the assignment statements to fair_type only exist indented inside the if statements.
That means if both conditions evaluate to false, neither of these assign statements run. So we need to initialize the variable fair_type so it exists in the standard case. Nice. It works. I would get started on the transit authority's next feature request, deducting bus fairs from the user's balance.
Video © Khan Academy, used under CC BY-NC-SA. Watch on Khan Academy. Puzzicle is not affiliated with Khan Academy.