0:00
Hey friends, welcome back to our Python
0:02
exercise series for beginners. If you're
0:04
just joining, don't worry, you can still
0:06
follow along. So today we are making
0:09
something fun and super useful, which is
0:11
a simple calculator, but this time using
0:14
smart if else logic which is built in.
0:18
So here is the plan. We'll ask the user
0:20
for the two numbers. Then we'll do four
0:22
operations and then we could be
0:25
addition, subtraction, multiplication,
0:27
and division. But wait what if the
0:30
second number is zero and what happens
0:33
when we try to divide it? So there is a
0:36
problem in you know division by zero
0:39
actually will crash our entire program.
0:42
So we will handle that nicely using if
0:48
All right let's go together. So we'll
0:51
build a simple calculator
1:07
So step one get user input.
1:23
enter the first number
1:29
and then to equal to input
1:47
as we are following the mathematical
1:49
operations, always always
1:52
change the type of the number the input
1:56
that you're getting. It is a string by
1:58
default. So we are changing it to float
2:01
because we are working on mathematical
2:05
And step two is to perform the
2:12
I'm going it fast because you guys have
2:15
already you know uh have gone through
2:17
all these steps and these are really
2:19
easy and simple to understand. So that's
2:22
why I'm not using I'm not going in deep
2:26
you know taking each step digging each
2:28
in step. So I'm just going it like we
2:31
have already done it before. So I'm just
2:37
which is num 1 + num 2
2:44
and then we are going to work on
2:50
which could be num one minus num two
3:01
and then we are going to do
3:02
multiplication. ation
3:15
So now we are going to handle the
3:18
division error. So here is where we will
3:21
check the division. So here is where we
3:41
I hope now you guys can see it. I'm not
3:44
wrong. And now let's check
3:58
So first we will check whether the
4:01
number is zero or not. The second number
4:05
is zero or not. So if num two not equal
4:11
to zero. So if that's not equal to zero
4:14
then it means it is something other than
4:28
So which is num one by num two.
4:35
else. That means if the number is zero
4:40
instead of you know if the second number
4:42
is zero then obviously if we don't print
4:45
anything if we don't do anything if we
4:47
just ignore it then our program is going
4:49
to crash. So instead of getting it crash
4:53
what we will do is we will add some
4:56
value to the division variable so that
4:58
it can print it for us. So which is
5:11
See now the division will print this
5:14
error if the second number is zero
5:16
instead of you know crashing the entire
5:18
program. So this is how we can handle
5:25
and now we are going to show the
6:33
So let's run this one. Enter the first
6:36
number maybe two and second number maybe
6:40
Addition 7 subtraction minus 3
6:43
multiplication 10 and division is four
6:46
and what if the second number is zero so
6:49
addition may be five the first number
6:51
maybe five I'm sorry second number zero
6:55
see division error cannot divide by zero
6:59
so this is how we can do it so first we
7:03
actually had two numbers as input so
7:07
which is the number one and number two
7:09
and then we store the results of
7:11
addition, subtraction and multiplication
7:13
directly. We didn't do anything. Even in
7:16
the previous exercise, we followed the
7:17
same thing and here also we did the same
7:19
thing. But coming to the third which is
7:22
the division. So that is really
7:24
important. So for division we actually
7:27
checked the whether the second number is
7:30
not zero whether the number is zero or
7:32
not. So if the second number is not zero
7:35
then we did just the normal division
7:37
which is num one by num two. What if the
7:39
second number is zero? So then the
7:42
program will come here and what if this
7:45
is zero? So 0 not equal to zero is
7:49
false. Right? So then it will generally
7:51
jump here to the else part. And then
7:54
here it will add division which is error
7:57
cannot divide by zero. And now the
8:00
program goes here. It will print
8:02
addition, subtraction, multiplication
8:04
and division. Coming to the division it
8:06
will take the input from here from this
8:09
calculations it could be either this or
8:11
this and then according to the
8:13
conditions according to the value of the
8:16
second number it will decide which value
8:19
we can assign to the division. So that
8:21
is how it will print error cannot divide
8:24
by zero if the number two is zero. If
8:27
the number two is other than zero any
8:30
other number then it will directly do
8:32
division which is number one by num two.
8:36
So this is what we have done. Maybe if
8:40
we don't do it, our program might may
8:42
crash and it just politely tells the
8:45
user that there is something wrong here.
8:50
So in the next exercises or in the next
8:52
upcoming concepts, we'll make our
8:54
program even more smarter. Until then,
8:57
you can just keep practicing, keep
8:58
coding, and I'll see you in the next
9:00
one. I hope you got this program done.
9:04
If you have any doubts, feel free to
9:05
reach out to me through comments. And
9:07
don't forget to like, share, and
9:09
subscribe. And also keep following us
9:12
for the next upcoming interesting