This video covers how to create a basic tip calculator in the python programming language!
A basic tip is determined by what percent of a tip you want to leave, as a proportion of the total bill! In order to create this function we need to gather user input asking for the desired tip percentage as well as the bill cost before tip. We then call a function to perform the tip math and display the outputs in a formatted f-string combining variables with strings!
This is a great beginner tutorial and if you're looking to use beginner python skills in creating applications, games and fully functionality utilities be sure to check out the rest of the channel! https://www.youtube.com/channel/UCV5Ab39YnXvTZ6Grar9URxQ
Let us know with any questions, comments, concerns or ideas for a future video!
Thanks for watching and be sure to like and subscribe for more great content!
what's going on you guys welcome back to
the channel and today's video is going
to be learning to build a tip calculator
in python
[Music]
so
the tip on a bill is not particularly
hard to calculate if you're used to
tipping 10
or 20
but um a lot of it today is like 18
or 22 percent
and really you probably want to build
this program to practice your python
more than you're going to whip out your
laptop after receiving a bill um so this
is more of a proof of theory proof of
concept maybe something you would add
into an application at some point
but the essential function of a tip is
you would enter like a
tip percentage
and then that would get
multiplied times the bill
and then divided by a hundred because if
you enter a percentage as a whole
integer like 18
you would want to convert that 18 to a
0.18 which is actually what you're doing
and we want to build a function that's
going to return this
and so we'll call it
tip calc and the two things that will
pass into the function and we'll show a
couple different ways of doing this but
the two things you would pass in would
be
we'll say tip percentage we'll call it
tip perk and then you want to pass in
the bill
so what you're going to return is just
what we said it's going to be the tip
percentage you got to spell it right
times the bill and then divided by 100
and that's it for the function so
getting that info from the user is going
to mean we would say like
tip
uh percent
because i like to use a different name
in the outside world than i use inside
the function
is equal to and then we'll make it a
float just in case someone wants to do
like 17.5
what's wrong with you psychos
um but we'll say float because if you
make that an int and someone enters a
decimal point it's going to error out
and we don't want that and if someone
enters a whole number this will still
work fine so
we'll do a float and then we'll ask for
a user input and we'll say
enter tip
percentage
and we will
tell them the units when you're getting
user info it's not a bad idea to tell
them the units to expect to enter it in
um
and so
we'll say bill in dollars equals and
we'll do the same thing float input and
then we'll say enter
the total
uh well we won't say total that might
confuse them and think it's after tip
let's just say enter the bill amount
and we'll throw a dollar sign in there
okay and then all we're gonna do
is call that function tip calc that we
already created
and for our tip percentage
the variable that we made was tip
percent
and then our bill
is our bill in dollars
okay and you don't need that colon and
this is going to return something but
since we didn't store it in a variable
then it's not really useful so
what we want here we'll call it tip
amount is what we're getting back from
the function
and let's go ahead and print two things
let's say print
your
tip
should
be and then the dollar
plus str of tip amount
and then let's
print your total
bill
would be and then the dollar amount
and then let's say the string of tip
amount
plus the bill right because those are
kind of the two fields that you fill in
on the bottom of the receipt is what do
you want the tip to be plus and it's
bill
dollars that's right
so
yeah those are kind of the two things
you fill in on the bottom receipt so
let's go ahead and run this make sure
that it's working and if it is we'll
take a look at some variables and some
things you can put in here to change it
up so let's say we had decent service
and we want to do an 18 percent tip
let's make the bill
amount an easy one just so we can make
sure our function is working properly
and let's see so the tip should be 18
yes and the total bill would be 118.
that's great and you could say well i
wanted to do 18 on 100 i don't think i
need a function for that all right let's
say you want to tip 22.25
because
reasons and you had a
76.50 bill okay
well it's telling you down to the penny
you could even round it if you wanted to
um i'm that's a lot of effort for a five
minute tutorial so your tip should be 17
dollars and two cents your total bill
would be 93.52 you can see right now
like that um that's a useful function
some people don't really understand how
to use their
phone calculators and it could be nice
to just spell it out for someone right
there and make it uh clean and simple
so
one other thing that you could do and i
kind of like this idea
you could put getting these
um variables inside of your function so
then when you call tip calc you're just
saying okay it's time to
um
it's time to calculate the bill uh i but
i don't want to
have to pass in variables
so you could get rid of those variables
you could put your input gathering
inside the function
and you can
you can get rid of the variables when
you call a function and so tip amount
now is just going to be the result of
tip calc and all we did is change when
you get the inputs but as i've said in
some other tutorials on this channel
a lot of times if you're writing
compartmentalized code as part of a
greater team you want to hand your
function off to the team and say all you
have to do with my function is run it
and know what you're getting back from
it so like hey run this i'll calculate a
tip for you what you'll be getting back
is a total tip amount um
so then all the person you hand the code
to needs is this line of code and then
they can use it just like this
now the limit to that is you're not
getting this
tip amount plus build dollars so what
you can do and what we're going to do is
we are going to go ahead and throw
all of this
inside of here
but instead of tip amount being equal to
tip calc tip amount is going to be equal
to this function
we don't need anything and then we're
going to go ahead and put those two
print statements inside the function too
so now literally all we have to do to
get the tip is just run
tip calc so
uh if you're familiar with functions if
you're not there's a tutorial on how to
create and call functions in python on
this channel
um but if you're familiar with functions
this def in python is not getting called
or looked at until it gets
referenced in this format so essentially
the only line of code that runs when
this thing starts up is this guy which
tells you to go into this function and
take a look at it but you can see when
we run it
error tip percentage let's say
16 on a
75 bill
okay well 12 87 it still works just the
same the difference is now you could add
this to some hierarchy of bookkeeping or
budgeting or some other larger function
um and one last thing we'll take a look
at you're probably you've seen this
before but another way to do these
strings
would be with f strings where you
the f stands for formatted but this is a
really great way to incorporate
variables with strings so
ordinarily if you have a variable
that is inside of a function you have to
do it like this you have to use str
and then you have to
convert any non-text variables into
strings using that str that can get
really tedious when you have multiple
variables and you have to use the plus
sign to concatenate everything
if you put an f before your string
that's saying anything i put inside of
curly brackets
i want to
i want the code to recognize that that
is a
variable but i want to format it as a
string so this is something that got
added in python
version three so if you are coding in
python version two you can end the video
disregard this bit if you want
but this right here is still going to
work just the same and now we've gotten
rid of needing the str functions and the
plus
concatenator so let's go ahead and run
that real quick let's say tip 29 knocked
it out of the park
on a
543 bill we were at a steak house okay
you can see it output looks just the
same but what we did is we reduced the
complexity of these string statements a
little bit so
those are just a few ways to improve
this i hope you found this beginner
tutorial project useful if you did check
out all the other ones on the channel
there's a ton of great content there
be sure to leave a like on the video
subscribe to the channel if you've been
enjoying what we've been making
and as always good luck with your code
and thanks for watching thanks bye