Python Exercise 05: Python Cashier App Tutorial: Calculate Total Price and Change Easily
Jul 19, 2025
Welcome to PyBeginners ā your go-to place to learn Python programming the easy way! š
Whether you're starting from scratch or revisiting the basics, this channel is built just for YOU. We break down Python concepts into bite-sized, beginner-friendly videos using real-life examples, everyday language, and fun visuals.
Think of PyBeginners as your coding buddy ā here to help you:
ā
Understand the why behind every concept
ā
Build confidence, one small step at a time
ā
Learn coding without feeling overwhelmed
š Check out more learning resources:
š Blogs: https://www.pybeginners.com/
š¼ LinkedIn: https://www.linkedin.com/newsletters/pybeginners-7267881715929415681/
š Free eBooks & PDFs: https://www.usandopy.com/livros/
š New videos every week ā covering Python basics, hands-on exercises, projects, and career tips!
š Subscribe, join the PyBeginners family, and let's make Python simple together š»š¬
#LearnPython #PyBeginners #PythonForBeginners #PythonMadeEasy
Show More Show Less View Video Transcript
0:01
Hey guys, welcome back to another
0:02
beginner Python challenge. And this one
0:05
is actually super practical because you
0:08
know when you go shopping, you buy few
0:10
items and you pay the cashier and get
0:13
your change back, right? So well today
0:15
we are going to build that logic in
0:18
Python.
0:19
Yep. This program will actually ask the
0:22
user the price of an item and then get
0:24
the quantity and how much money they
0:27
paid.
0:29
So, and then it will calculate the total
0:32
cost and tell them how much change they
0:34
should get back and let us break it
0:36
down. You don't have to worry about what
0:38
you're going to do because we are going
0:39
to break it down using this particular
0:42
flowchart and we will write a clean
0:44
Python program step by step.
0:47
So, the first thing we have to do is to
0:49
understand the flowchart logic. So,
0:51
here's what the flowchart for this
0:53
exercise looks like. We will start and
0:56
we will get the price per unit. We will
0:58
read the quantity and even the amount
1:01
paid. And then we will calculate this
1:03
total cost using this formula. You don't
1:05
have to crack the formula. It's already
1:07
there. You just have to implement it. So
1:09
it's quantity into price per unit. And
1:11
then you will calculate the change as
1:13
amount paid minus total cost. And then
1:17
you will display the quantity of items
1:19
purchased and total cost and even the
1:22
change. And that's the end.
1:24
It's pretty simple. Let's go through
1:27
this carefully so that you really get
1:29
how each part exactly works in Python.
1:32
And yeah, let's get the user inputs
1:35
first.
1:37
So all you have to do is to get the
1:39
price per unit. So let's just give price
1:43
per unit.
1:47
Then put
1:53
enter the price of an item
2:02
and then
2:05
we have to get the quantity
2:11
it's input.
2:14
Enter
2:16
the quantity
2:20
of
2:22
items purchased.
2:32
Mhm. And then we need amount paid
2:43
loach
2:56
input.
3:00
Enter the amount paid
3:05
and also give these things in the
3:07
desired input you want. I want quantity
3:10
to be an input integers and price per
3:13
unit should be float.
3:17
Okay. So now we have got our inputs and
3:21
they are in the correct format to do the
3:23
math. So now let's calculate the total
3:26
cost and change. So first let's
3:29
calculate the total cost. So total cost
3:33
equal to
3:36
quantity
3:38
into
3:40
price per unit.
3:45
Okay. And then we have to calculate the
3:48
change
3:50
which is amount paid minus total cost.
3:53
But wait, we should only calculate
3:55
change if the amount paid is enough to
3:58
cover the total cost. Otherwise, we can
4:01
show a friendly message something like
4:03
this.
4:06
So
4:08
in order to check in order to calculate
4:10
the change if the amount paid is enough
4:13
to cover the expenses then here we are
4:16
going to use if condition. You don't
4:18
have to worry about what is this if now.
4:20
It's just a condition that we will
4:22
check. So we are just checking the
4:25
amount is enough or not. So that is why
4:28
we need this if condition. Now don't
4:30
think that if is something new I don't
4:33
know how to do it. You don't have to
4:35
worry about that. Just blindly follow
4:37
that. Just blindly follow the syntax. If
4:41
amount paid
4:44
is greater than or equal to total cost.
4:48
If the user give the paid amount is
4:51
equal to or is more than the total cost
4:54
then only we can calculate the change
4:56
right it's basic so change equal to
5:00
amount paid minus total cost
5:05
if not what if the user didn't give the
5:08
enough change enough amount so you just
5:11
have to print this first
5:14
saying that
5:16
and I'm using frings here.
5:19
So your change is
5:28
change I want it in decimal so 2F
5:31
mandatory
5:33
then rupees
5:37
okay what if the amount is not
5:40
sufficient so then we will go to else
5:44
so just print
5:47
insufficient ient
5:52
amount
5:54
paid.
5:56
Please provide
6:00
enough
6:02
to cover
6:05
the total cost.
6:12
And yeah, this will actually help us
6:14
prevent errors and makes our programs
6:17
feel smarter.
6:20
Okay, so here what we did if we got the
6:22
enough amount or if we got the more
6:24
amount than the you know actual cost
6:26
then we can calculate the change and
6:28
this is the formula to calculate the
6:30
change and here you are printing the
6:33
calculated change as output. If the user
6:35
did not give the sufficient amount then
6:38
we will print insufficient amount paid.
6:40
Please provide us enough to cover the
6:42
total cost. So that is what we are going
6:44
to you know show show it to the user so
6:48
that even user will think like oh I have
6:50
to add more amount so I have to give
6:52
more money. Okay now let's display all
6:56
this clearly.
6:58
So I'm going to display it here. So just
7:01
have to print
7:05
it's like a receipt. So I'm using slash
7:08
which is nothing but it will help the
7:10
Python program to print the output in
7:12
the next line.
7:15
So purchase summary
7:22
and then you're going to print
7:26
again I'm going to use a strings here.
7:32
So quantity purchased
7:45
quantity
7:48
and print
7:56
total cost.
8:03
And we need this in decimals
8:06
2F
8:08
and
8:12
okay
8:15
so this is what we are going to do. So
8:18
then inside the if block we show the
8:20
change if not enough is paid we already
8:23
print a warning. So this gives a really
8:26
nice user experience like a mini bill.
8:30
Okay. So here is the full program. So we
8:34
have got three inputs from the user
8:37
price per unit, quantity and amount
8:39
paid. And then I'm I'm actually
8:41
calculating the total cost. And then we
8:44
are actually showing the summary to the
8:46
user. And then we are calculating the
8:49
change because if user gave the expected
8:52
cost then it will calculate the change
8:54
if not then it will show insufficient
8:57
amount paid.
8:59
So it's pretty neat. You can run this
9:01
program, enter some values, and get a
9:03
full checkout summary like you're
9:04
building a point of sales system. Let's
9:06
try.
9:15
So the price of an item 20 rupees maybe
9:19
and the quantity is three and the amount
9:22
paid maybe 75.
9:26
So the quantity purchase three items
9:28
total cost 60 rupees and your change is
9:30
15 rupees. It's pretty simple. And what
9:33
if you give insufficient amount and the
9:37
enter the price of an item maybe 30
9:40
rupees
9:42
and enter the quantity two but you just
9:46
paid 30 rupees.
9:48
So the quantity purchase two and total
9:50
cost 60 rupees but insufficient amount
9:53
paid please provide enough to cover the
9:54
total cost. So this is what user will
9:56
see.
9:58
So the quick pro tips for beginners. So
10:03
before we wrap up, here are few you know
10:07
some quick tips that you should follow.
10:09
I would suggest always you to convert
10:11
your inputs with float or integer
10:14
depending on the data that you have and
10:16
always try to use 2F in fst strings
10:20
especially in order to format the money
10:23
or decimals and always check if the user
10:27
entered enough money. Don't just assume
10:29
that user will enter the amount that
10:32
you're expecting. So always see what's
10:36
wrong, what might get wrong and then
10:39
accordingly you have to write the code
10:41
and also keep your output clean and user
10:45
friendly especially for real world use
10:47
because if you don't print this message
10:50
how user will understand that he
10:52
actually gave insufficient amount. So
10:54
that's how you have to print the
10:57
expected you know result according to
11:00
the situation.
11:03
And yeah, that wraps up exercise five.
11:06
And we built a real world checkout
11:07
calculator using just a few lines of
11:09
Python. And if this helped you, give the
11:12
video a thumbs up, hit that red
11:14
subscribe button, and turn on
11:15
notifications so you don't miss the next
11:17
Python challenge. And we are learning by
11:20
doing. And that's the best way to master
11:22
coding. And thanks for watching, and
11:25
I'll see you in the next one.

