Transcripts
1. 01 Simple calculator introduction: This video series, we want to
create a simple calculator, so it can get two
numbers from input, and it can do operations
like addition, multiplication,
subtraction and division. So let's begin together.
2. 02 Get numbers and operation from input: In this project,
we want to create a simple calculator that get two number and we can tell it if it should add it together, subtract it together,
multiply together, or divide them together. And we can't divide a
number with the zero, so we need to consider that. So you try to do that, and we will do it
inside these videos. For doing that, I want to
go to or desktop in here, right click and
create a new folder, and I want to call it
simple Calculator. That's it. Put that over here. Now, in VS code, I want to go here and
open up that folder. In desktop, it's called simple calculators,
select folder. And now in here, I want
to create a Python fold. So click in here and
I want to call it simple calctor dot PY, right? So with that done, let
me zoom a little bit. So in here, let me make
this a little bit smaller, so you will see
everything in here. First thing that I
want to do in here, I want to tell the user what
this application is about. So I will say print, and in here with a double
quotation, I will say, welcome to the simple
calculator, right? And after that, I want
to print something else. I want to say the calculator can perform simple calculations, and we can show plus after that minus and after that multiplication and
after that division, right? So with this two line, we tell the user what
this application can do. Now, let's see how
we can run this. We can go here, go to terminal, and create a new terminal. And in here, we can say
PY simple head tab. After that, it will put
a there, head Enter. And now you can see it says, welcome to simple
calculator and after that, it says, the calculator can
perform simple calculation. Now next thing that
we want to do, we want to get some inputs. For doing that, we say input, open and close parents, and inside these open
and close parenss we need to tell what
to enter, right? We call it enter
the first number after that a colon and a space. So it looks much better. And this input is
giving us a string. That's why we need to
change or convert what this input return to a float number or integer number based on
what you want to do. But I want to
change it to float. So we say float. After that I open and close
prances around over input. Now, what this will give us, it will get a number
from the user, and it will convert
it to a float. And after that, we can
save it inside somewhere. We say num one is
equal to float. Input inter the first number. Now with that, if you run, what will happen, it says, Inter the first Number,
for example, ten, and after that, the application
will be finished, right? No, after this, we need
to get the operation, what operation
user wanted to do. And for that, we
need input as well. I will call this one
operation is equal to input, open and close parents. And in here, I will say
enter the operation, right? And as a guide, we can use open and close
parenss to tell it either plus or minus or
multiplication or division, you can do with this
application, right? And after that, I want to
add the colon and space, so it will looks much better. Now, if you run, what
will happen first, it will tell us to
enter the first number, for example, ten,
and after that, it will tell us enter the
operation, for example, plus. After that, the application
will be finished. But know what you want to do, we want to get the number two. So either we want to
add them together, subtract them together, multiplication or
division, right? So in here, I will say num two, and because we need a
float or integer value, I will say float and after that, you want to get it from input. We say input, open
and close parenss. And in here, I can say
enter the second number, right after that
colon and space. K with that done. Again, if we run,
what will happen, first, it needs us to
provide the first number, for example, ten,
after that operation, let's just say plus,
and after that, the second number,
let's just say 20. It doesn't do anything. I want to give you
as a challenge. Check the operation and
based of the operation, return or show the result. You try to do that. We'll
do it in next video.
3. 03 addition operation: Know, based on the operation
that the user choose, we want to print the result. So in here, what I want to do I want to create a variable, I want to call it result that is equal to zero by default. And know with that
done with the series of Is LF and all of that, we want to check the operation. So in here, we can
say if operation was equal to plus
after that colon, we want to say
result is equal to num one plus num two, that's it. If the operation is plus, it means we can add these two number together and
put it inside the result. And after doing that, we can print the result, right? So we say print. Let's
just use the FS string. And in here, we want
to say the result is open and close curly brackets and show the result. That's it. No, let's just test that. If we run first, enter the first number
ten, for example, after the operation, let's
just use plus for no. After that, the second number, let's just use 20, and you can see it says
the result is 30. So now we have done
deep adding operation. Now I want to give
you the challenge for subtraction and
multiplication. Try to do that yourself. We will do it in next video.
4. 04 substraction and multipication: No, we want to be able to
do the other operations. So for doing that, we did use operation is
equal to plus, right? So what I want to do in here, I want to use AI, right. And after that, I
want to check if operation is equal to minus. After that, colon, we want to say result is equal num one, minus num two. That's it. We need another one
for multiplication. So let's just do it in here, L if, and we want
to say operation. L if, if operation is equal
to multiplication, right? In that case, we want to say
result is equal to num one, multiply by num two. That's it. With these lines, we can add, we can subtract
and we can multiply. So let's just test that. If we run in here, first number, let's just say 20, and let's just say minus
this time, and after that, let's just put ten in there, and the result will be ten, 20 minus ten, it
will be ten, right? Again, let's just run it. For example, this time, let's just say three for
the first number, let's just say multiplication. And after that, let's
just use eight, for example, head inter, and the result is 24. So now we create a
application that can add addition, subtraction,
and multiplication. But for division,
we need to check if the number two is zero.
We can't do that. We can't divide a number
by zero value, right? So that's why I want to give
you this as a challenge. Don't let division by zero, and you try to do that. We will do it in next video.
5. 05 Division: No, we want to do the division. For doing that, I want to
use another ALIF, I, right? We want to check
the operation if it is equal to division, right. In that case, first, we want to check if the
number two was equal to zero, give the user error that
you can't divide by zero. So in here, I will check with
an F inside the LF, right? So we check with an F number
two was equal to zero. In that case, just use a
print and say, for example, error, you can't
divide by zero, right? That's it. No after this on exit out of this
program, right? So if they choose
number two to be zero and they choose
division, in that case, we want to say error, you can't divide by
zero and after that, exit out of the program, right? But in the else case, it means when the
num two is not zero, this else will happen. And in here, we can
say result is equal to number one
divided by num two. That's it. Let's just save. In here, run, for example, let's just save 20
for the first number, and let's just say division. We want to do the division. And after that, for
the second number, let's just use five, and it will give us four and
everything looks awesome. No, let's just run
it one more time. If you put 20 for the first
number, after that division, and after that, use zero
for the second number, it will give us an error. Error, you can't divide by zero. So now we have a
simple calculator.