0:00
Hey everyone, welcome back to Simplified
0:02
by Singum. Welcome back to our Python
0:04
for beginners series. In today's
0:06
exercise, we are going to write a very
0:08
colorful program. We'll build a program
0:11
that checks a number you enter and tells
0:14
whether it's blue, red, green, or just
0:18
not a valid option. Sounds cool, right?
0:21
Let's jump in. But before coding, let's
0:24
look at this flowchart. And also
0:27
remember as we always say flowcharts are
0:30
like maps that guide our program's
0:34
So here is how it works.
0:36
First user enters a number and if the
0:40
number is between 0 and 10 then the
0:43
program prints blue. If it's 10 if it's
0:46
in between 10 and 20 then it prints red.
0:49
If it is in between 20 and 30 then it
0:53
will print green. But if the number
0:56
doesn't fall into any of these ranges,
0:58
then the program politely says that's
1:01
not a correct color option.
1:03
So basically the program checks each
1:05
range step by step until it finds where
1:11
Okay. Now let's bring this flowchart to
1:14
life using Python. As always, all you
1:24
Just create a variable called n. And I'm
1:27
giving float because we are going to
1:28
take the number and then input.
1:32
I'm going fast because by this time if
1:34
you have followed me from beginning and
1:37
if you have followed each and every
1:39
exercise with me then I think you might
1:41
followed my pace as well. So that's why
1:43
I'm going a bit faster instead of
1:45
explaining because I have explained
1:47
these before. So that's why I'm going a
1:50
bit faster. And now it's time to work on
1:57
And so if you observe the flow chart,
2:01
the range of n should be in between 0
2:03
and 10. Then only you can print blue.
2:07
So if 0 less than n less than 10
2:13
is n in between 0 and 10 that is what
2:17
this condition is checking and then if
2:19
it is in between 0 and 10 then you will
2:25
Okay. In the same way,
2:32
n is between 10 and 20. How you check 10
2:40
n less than 20? Then you will print red.
2:55
n is between 20 and 30? So obviously you
3:00
will print green. So again l if
3:05
20 less than n less than 30
3:33
First, we ask the user to enter a number
3:36
and convert into a float. So, the
3:39
program can handle decimals too. And
3:43
then comes the magic of conditions. If
3:45
the number is greater than 10,
3:51
if the number is greater than zero and
3:53
less than 10, simply if number is in
3:56
between 0 and 10, then you print the
4:04
If number is greater than 10 and less
4:06
than 20, if it is between in between 10
4:09
and 20, then you print red. If it is in
4:12
between 20 and 30, then you print green.
4:14
And if none of these are true, then
4:17
Python goes to the else part and sees
4:19
this is not a correct color option. So
4:22
the program works exactly like the
4:25
flowchart checking one condition after
4:30
Let's test it with a few examples.
4:39
For example, let's give a number maybe
4:41
eight and see it is giving blue. Why it
4:45
is giving blue? So when you give 8, the
4:49
Python will come here and it will check
4:51
whether n is in between 0 to 10 or not.
4:55
If it is in between 0 to 10, then
4:57
obviously it will print blue. Then it
4:58
will come out of the entire program. It
5:00
don't have to run these conditions.
5:02
Right? So that is what it is doing.
5:06
What if you give number 50
5:10
red? So first the Python interpreter
5:12
comes here. It will check whether n is
5:14
in between 0 to 10 or not. So here you
5:18
gave 15. So n is not in between 0 and
5:20
10. So now it comes to the l if part. It
5:23
will check whether n is in between 10
5:25
and 20. So the value of n is 15. So 15
5:29
is in between 10 and 20. So this
5:31
condition is true. So then it will print
5:33
the output as red. Okay. What if you
5:43
It is printing green.
5:46
So, same thing. It comes checks in
5:49
between 0 and 10. 25 is not in between 0
5:52
and 10. This is false. Now, it jumps
5:54
here. 25 is not in between 10 and 20.
5:58
So, it jumps here. 25 is in between 20
6:02
and 30. So, it will print green.
6:07
And now what if the value is 45?
6:11
This is not a correct color option
6:12
because we gave the range from 0 till 30
6:16
but not 45. Right? So that is why we
6:19
just printed it as this is not a correct
6:21
color option. See the program works
6:24
perfectly just like our flowchart
6:29
Great job guys. You just learned how to
6:32
connect flowcharts with Python code to
6:34
solve real world problems if you have
6:36
followed these videos and previous
6:37
episodes along with me. And I love this
6:40
one because it shows how numbers can
6:43
belong to different categories. Now,
6:45
here is the deal. If you are enjoying
6:47
these step-by-step exercises, don't
6:49
forget to like, share it with a friend
6:51
who is learning Python, and of course,
6:54
subscribe to the channel so you don't
6:55
miss the next colorful coding adventure.
6:58
Remember, every single exercise you
7:01
complete makes you a stronger and more
7:03
confident Python programmer. I'll see
7:05
you in the next one. Until then, keep
7:07
coding, keep smiling, and keep coloring
7:09
your logic with Python.