Hey guys, welcome to the final project of today.
And in this final project, we're going to be building a Tip Calculator.
So the final output is going to look like this.
It's going to print,
"Welcome to the tip calculator."
And it's going to ask you for an input for how much the total bill came to.
Let's say that we and a couple of friends went out for lunch, and it came to $124.56, and then it'll
ask you, "What percentage tip would you like to give?"
So let's say we want to give a 12% tip.
So we enter 12 and hit Enter.
And then finally it asks, "How many people are splitting this bill?"
Let's say there's seven of us and then hit Enter.
Finally, it's going to tell us that each person should pay about $19.93.
Notice how the final bill, even though after all of these calculations, it's probably got more numbers
after the decimal point, we only want two decimal places of accuracy, so it should be rounded to two
decimal places.
The second thing to remember is that these are percentages, and in order to calculate a percentage
of something, you can multiply a number by the percentage number divided by 100.
Let me show you this in a little bit more detail, because this project is not about testing your maths,
it's about seeing how well you've understood the programming concepts.
So let's say that we had a bill of $150.
If we were to apply a 12% tip on top of that, then 12% is going to be equal to 12 divided by 100,
which is equal to 0.12.
Now the next step is we can multiply 150 the total bill by 0.12.
And this will give us what 12% of 150 is, which is 18.
Now we of course have to add the tip onto the final bill so it becomes 150 + 18.
So $150 with 12% tip is equal to $168.
Now a shorthand way of doing all of this is simply multiplying 150 by 1.12.
So the one is 150, and then it's 0.12 on top of that.
So when we do this, it gives us pretty much the same number.
Now if during your testing and your coding, you come across some sort of number that looks a little
bit strange because you think 150 multiplied by 1.12 should actually equal 168
precisely,
so what are all of these extra numbers at the end?
Now, the short answer is you don't have to worry about it.
It's simply related to how Python processes these floating point numbers.
If you're really interested and you want to read about this, then I'll link to this page in the Python
documentation where they tell you why this is happening,
but be warned, it's pretty dense and it's pretty heavy.
But the final conclusion is it's just the way that Python is approximating this number, so you don't
actually have to worry about this.
Now, once we've gotten to 168, the next step is to split it between five people.
So, 168 / 5 = 33.6.
Now what we want to show the user is we want to show them the amount that they have to pay with two
decimal places of accuracy.
So we want to say something like 33.60 if this is the case, because that's normally how we represent
numbers when it comes to money.
So what I want you to do is to also be able to round any of these numbers to two decimal places.
If you have successfully created this program, then it should work exactly as this example version
would, and you'll be able to see the formatting, where it actually tells you each person should pay
this particular dollar amount, and it's rounded to two decimal places.
And this is the amount, after taking into consideration these three different inputs from the user.
Have a play around with the final product, and then have a think about how you would create this program
step by step, and then try to achieve this result.
This is going to test everything that you've learned so far, including f-strings, including complex
mathematical operations including PEMDAS and calculating numbers, and the order in which mathematical
operations are run by a computer, as well as everything you've learned in the previous lessons, and
previous days as well.
I think this is going to be a lot of fun, and the end result is going to be kind of useful the next
time that you need to split the bill between friends.
So I'm going to let you pause the video and get on with it.
And then once you're done, if you want to, head back over here and I'll walk through the solution with
you, but I really recommend you giving it a good go before you come back.
So pause the video now and I'll see you later.
Okay, so we've seen how the program is supposed to work, and we're trying to create it from scratch
by reverse engineering it essentially.
Now the first thing our program should say is, "Welcome to the tip calculator!"
So that's easy enough.
We just have to create a print statement.
Perfect.
And if you're creating a really enthusiastic tip calculator you can replace the full stop with an exclamation
mark.
Okay, so let's run it step by step to make sure that we don't have any errors along the way.
So the first step seems to be working.
Now the next step is to ask the user for some input,
and we want to know what was their total bill.
Where did it come to?
So let's go ahead and create an input and ask them, "What was the total bill?"
And then we'll add a dollar sign or whatever currency it is that you prefer to work with
because when we actually run this, you'll see that the input will go straight after the end of that
string.
So, add the dollar sign.
So now we can enter a dollar amount like this,
but it's not really saved anywhere.
This data just disappears because we haven't stored it inside a variable.
So let's go ahead and do that.
Let's call this variable, bill.
And we'll set it to equal whatever the user typed in to this input.
Now remember that this bill is going to have a data type of a string.
Right?
So if I go ahead and hit run and let's just type some numbers in here, you'll see that the data type
of the bill is, as we said, a string.
In order to be able to do maths, you might remember we have to change this into a number format.
So a float or an int.
Now in this case, because the bill is likely to have numbers after the decimal place, it's probably
better that we turn it into a float so that we get the most accurate result possible.
Now that we're done with the bill, the next question the tip calculator should ask is what percentage
tip would you like to give,
10, 12, or 15?
Or you can of course switch this up depending on how much tip you normally give.
So the next line is going to be another input.
Here I'm asking the user, "How much tip would you like to give?" and giving them some examples.
And notice how in my examples, I don't actually have the percentage sign here, because I don't really
want them to type the percent sign into this input, because if they do, when they say 12%, this is
going to be really hard for me to turn the 12% into an actual number.
So you could, in your input message, say something like, "How much tip would you like to give?
10, 12 or 15?"
Please don't add any percentage signs, just add the number or something like that.
But again, once we've done this, it's not stored anywhere unless we add it into a variable.
Now we've got a bill that stores the total bill as a float.
And we've got a tip which is going to be a whole number, 10, 12 or 15.
Right?
So depending on what you think is best, you can either turn this into a float or in my case I'm probably
going to convert it into an integer.
So now we're on to the final step which is how many people to split the bill between.
So let's go ahead and create a variable called people.
And I'm going to add an int wrapper around this input so that even in the very beginning we know that
we have to convert the number of people into a whole number,
because I've never had a meal with 3.5 people before.
So the input is going to be, "ow many people to split the bill?"
And at this point, they should enter the number of people which will turn into an integer stored inside
this variable called people.
So now that we've got all the data collected from the user, we're finally ready to do some maths.
And we said that the way to work out the tip is by multiplying it by 1 point, and then the number
after the decimal point is whatever percentage they decided to give.
So it would be 1.1 if it was 10% and 1.15 if it was 15%.
Let's go ahead and calculate the bill_with_tip.
So remember how we said that before the tip percentage is equal to the whole number 10, 12 ,or 15 divided
by 100 which turns it into 0.1, 0.12, or 0.15,
and then once this calculation is done, we can multiply it by the bill.
And then finally we add that to the original bill.
So let's go ahead and print this and just check to see that it looks sensible.
So bill_with_tip is what we're going to print.
And let's put some easy numbers in there.
Let's say that the bill was $100 and the tip was 10%,
so then the 10% of $100 is $10.
So the bill_with_tip,
that's what's going to be printed next, it should be 110.
So we can type anything in here because we're not using it,
and you can see that this calculation, bill_with_tip is 110, which is what we want.
Now there's a lot of other ways that you can express this.
For example, you could say bill * (1 + tip) / 100).
So this would actually give you the same result as well.
But remember what we said previously about how Python is a little bit weird with floating point numbers,
but essentially this is still giving us the same result, which is 110.
So you can choose whichever way you find most intuitive, and if you want, you can even split this
up into several steps, right?
We could say that tip_as_percent = tip / 100.
And then we can multiply the bill by the tip_as_percent to get the total_tip_amount.
And then finally we can get the total_bill by adding the bill to the total_tip_amount.
Feel free to do this whichever way makes sense to you, but I'm going to leave it with as many steps
as possible so that you can actually work through the logic if you got stuck on this maths.
Now, the next thing we need to do is to divide the total_bill by the number of people.
So if we had five people, then we would divide it five ways, right?
So let's call this bill_per_person, which is going to be equal to the total_bill divided by the number
of people.
Now at this stage, this bill_per_person is a floating point number,
so it could have many many digits after the decimal point.
If we want to round this to two decimal places, then you might remember from some of the lessons today,
we have access to a function called round(),
and here we can add the number that we want to round which is the bill_per_person,
and then after a comma, we can specify how precise, how many numbers or how many decimal places do we
want to round this bill to.
And in our case it's two decimal places.
So now this is the final amount which I'll call final_amount, which is the bill_per_person rounded
to two decimal places.
And we can now finally print this to the user and say, "Each person should pay..."
And remember, you can either use string concatenation, which will require you to convert this number
into a string again, or use the trick that we learned in the previous lessons, where we can create
an f-string by adding f in front of the string, and then using some curly braces to insert the {final_amount}
right here.
And we can write the "{final_amount} dollars..."
Or we can put a dollar sign in front and maybe a colon here and now if we clear our console and run
our code then you'll see it work.
So, "Welcome to the tip calculator!
What was the total bill?"
Let's make up some random number.
"What percentage tip?"
Let's say we're going to give 15 and then we're splitting it between five people and it calculates everything,
doing all of that maths.
And finally, it rounds it to two decimal places, giving us the final amount each person should pay.
How did you get on with this project?
Did you manage to get it in one go without having to look at the solution?
If you got stuck at any point or if your code didn't work as you expect it to, this is the time to
go back to it, fix it, and make sure that you've understood everything that's been covered through
this exercise.
So I hope you had fun building this project and doing all of the coding exercises and learning about
mathematical operations, f-Strings, Data Types, Type Conversion, and all of the things that we did
today.
Have a rest and I'll be back tomorrow
teaching you about Conditional Statements, and how we can get the program to do different things depending
on certain conditions, or certain parts of our code.
So I bid you good night and I'll see you tomorrow.