0:00
Hey guys, welcome back to this channel.
0:02
In today's quick Python exercise, we are
0:04
going to catch and fix a very common
0:06
beginner mistake. One that has to do
0:09
with string concatenation when
0:11
calculating the average of numbers.
0:14
Trust me, if you're new to Python, you
0:16
have probably run into this without even
0:18
realizing it. So, let me show you the
0:24
So let's write a program
0:54
So let's write. So number equal to input
1:03
enter the first number
1:13
this will be number one.
1:26
Enter the second number.
1:52
And now in order to calculate the
1:56
we are going to take a variable called
2:00
and just give number one plus number two
2:42
So it's looks simple, right? But guess
2:44
what? This code will throw an error. And
2:47
can you please spot the issue?
2:50
I want you to pause the video here and
2:58
Okay, so here is what wrong. The input
3:01
function in Python always returns a
3:04
string. Even if you type in a number, so
3:07
when the code tries to add number one
3:09
plus number two plus number three, it's
3:12
actually joining three strings and not
3:14
doing any math. and it gets worse and
3:18
then it tries to divide the string by
3:21
three which Python doesn't allow and
3:24
it'll lead us to a type error.
3:29
So if you want we can do this right away
3:32
and we can run this and we can check.
3:36
So like for example 1 2 3 see you see a
3:46
unsupported operand type is string and
3:51
So how do we fix it? We simply need to
3:55
convert those inputs into numbers using
3:59
float or int depending on whether we
4:02
want decimal support.
4:06
So here is the fixed version. So I
4:09
sometimes you know when it when you're
4:11
dealing with averages you need inputs in
4:15
float. So I'm converting the input from
4:33
So here's a fixed version
4:36
and notice that we are now using float
4:39
in order to convert each input and that
4:42
means if a user enters 10 20 and 30 we
4:46
actually get numbers not strings and we
4:48
can safely perform math on them.
4:52
And let's run this code now. And enter.
5:07
So enter the first number 10 20 30. So
5:12
here I'm again concatenating
5:16
and this is actually an integer or it
5:19
could be a float number and I'm trying
5:21
to concatenate it with the string and
5:23
that's where we can do wrong. So you can
5:26
either do something like you can do
5:28
something like this you can convert the
5:31
you know it to string and then you can
5:33
try doing it and then it'll work this
5:39
See uh or you can also do
5:43
can remove this and you can just add
5:48
and then you can do this.
6:14
That's exactly what we wanted. And just
6:16
like that, you have learned a really
6:18
important debugging skill. And always,
6:21
always check your data types, especially
6:23
when dealing with user input. And if you
6:26
like this video, make sure to like,
6:28
subscribe, and share this with your
6:29
fellow Python learners. And drop a
6:31
comment if you have ever made this
6:32
mistakes before. So see you in the next