0:01
Hey everyone, welcome back to another
0:03
episode of our Python exercise series.
0:06
And today we have got something super
0:08
simple but often tricky when you are
0:10
just starting out. We are going to fix a
0:13
small bug in a Python program that
0:15
checks whether a number is even or odd.
0:19
So here is the code. So let's give
0:23
number equal to input
0:36
and here we are actually checking
0:40
whether a number is even or odd. So
0:43
whenever we say a number is even it
0:46
should actually divisible by two right.
0:49
So how we check that in the programming?
0:51
So we will just check whether the number
0:54
is divisible by two and the remainder is
0:56
zero or not. So we can use the mod
0:59
operator. If you still have confusions
1:01
regarding the operators how every
1:04
operator exactly works and how this
1:06
modulus works you can just go to the
1:08
operators tutorial and just have a
1:11
glance at it and then you can come back
1:13
and you know continue learning.
1:16
So if the remainder is zero then we can
1:20
say that a number is even right. So
1:30
else obviously it'll be an odd number.
1:33
So result equal to odd.
1:39
So now we can just ask the Python
1:47
whether the number is even or odd.
2:02
So at first glance this might look fine
2:05
but if you try to run it Python will
2:08
throw definitely an error. Why? It's
2:12
because I want you to guess. I don't
2:14
want to say it right away. I want you to
2:15
guess and go through the code once. Give
2:19
it a pause and go through it and just
2:21
check where we are doing it wrong.
2:26
It's because input here always takes
2:31
input as a string not a number. Right?
2:34
Here is the actual buck. We are trying
2:37
to use the modulus operator
2:40
on a string and which is not at all
2:43
allowed. So we need to convert the input
2:45
to an integer before doing any
2:48
mathematical operations.
2:50
So if you observe all my exercises all
2:54
of them are most of the time with the
2:56
input because that's where beginners do
2:59
the mistakes most of the time that's
3:01
where beginners will not understand why
3:04
there is an error even I am when I'm
3:06
when I was a beginner I used to like I
3:10
don't even get it why the program throws
3:12
an error on my face what's wrong with
3:15
that so that is when this type
3:17
conversion is really important
3:20
So we need to convert input into an
3:23
integer. So let's do it.
3:28
Just give it problem solved.
3:33
So I think the rest of the code is just
3:36
fine. And if the user enters the seven
3:39
then the output will be
3:42
odd. You're right. It's odd.
3:46
So yeah, I think it will work and let's
3:55
Like for example, if we give the number
3:57
seven, the number is odd. What if the
4:00
number is an even number?
4:02
There's something wrong with the
4:03
interpreter. I don't know why.
4:07
Okay, let's close this.
4:14
And now let's give even number. The
4:16
number is even. So these little bugs are
4:19
very common when you're starting out. So
4:22
don't worry if you have made a similar
4:24
mistakes like before. Just remember
4:27
always always check your data types. A
4:30
string is not a number until you make it
4:32
one. So if you found this video really
4:36
helpful, don't forget to like this video
4:38
and subscribe for more beginner friendly
4:40
Python fixes. And let me know in the
4:42
comments if you have ever made this
4:44
mistakes. See you in the next one. Keep