This is the solution of the exercises for the level 2 of this course.
So the first exercise is actually here. You have
to ask for the name and age of a user and print
the info. So basically that's the function.
So I'm not going to rewrite it one more time.
What I advised you to do was to actually try to
rewrite it by yourself and see if you can get
this also. Okay. Now I'm going to go to the
second exercise which is to ask the user for
two integer numbers, add them, print the result.
So I'm going to create a new Python file here
and let's say add to it. I'm going to name it
add to it. So I'm going to create a function
which is going to add two numbers. So add
two numbers. Okay. You can use numbers inside
function names. Just make sure you don't start
with a number. And well, for each word you add
an underscore. And then I will need two parameters
here. I will need number one and number two.
So I can use A and B here. So I told you to
use meaningful names and maybe avoid A and B.
But if you just need to add two numbers, using
A and B actually makes much more sense. Then I'm
just going to do return A plus B. Okay. This is
very easy to read. So this function is quite easy.
We just receive two parameters. We return the same
of those parameters. Now I'm going to go back to
the normal invitation and I'm going to do input.
Enter number one. Okay. And then input. Enter
number two. In order to keep this and be able
to do something with that, I'm going to save it.
So number one is equal to input. And then number
two is equal to input. Okay. I'm going to save
those into variables. And because the input
is returning a string, I need to cast it with
int like this. Okay. Int number one. Int number
two. I have my number one, my number two.
Now what I can do, I can do print. So I'm going
to do all that in one line. And let's say str
number one. So now that I have casted number one, so the input into the integer,
if I want to print it, I need to cast it again to a
string. Okay. So number one plus and then the plus
sign plus str number two plus and then equal,
then plus. And I can call the function add two
numbers. I can use the autocompletion. And I'm
going to give number one and number two. As you
can see, the line starts to be quite long. So
what I can do, I can after here, after this,
I can come back to a new line. And this is going
to work. So make sure you have the correct amount
of parentheses. And if you have an instruction
that is quite long, well, you can cut this. Okay.
So if you have a plus, for example, you can cut
at the plus, just go back to a new line with some
indentation. Okay. It's going to automatically
indent this. Actually, the number of indentations
here is not important. Okay. Or if you have a
comma, this is also a good place to cut the line.
If you have a comma, you can just go back to a new line. It's going to work also.
So now I'm going to run that, actually add two
ints. So I need to run the dot add two ints.
And turn number one, two, and turn number two, five.
And we have an error because I also need to cast
the results as a string. Okay. The result is an
integer. I need to cast the result as a string.
I run add two ints. So enter number one, three,
number two, four. And we can see three plus four
is equal to seven. So that's the program. Okay.
Now let's go to the number three. We have to create
a list of float numbers, compute the average,
and print the average. So again, new Python file.
Let's say compute average from list. And I'm
going to create def compute. So let's name it
compute list average. Okay. And number list.
Okay. So one function which is named compute list
average with one parameter, which is the list.
And I can do return sum number list divided by
length of number list. Okay. I compute the sum.
I divide by the length. And this is a function.
So now I can use this for any list I have. So
number list is equal to, let's say, three, four,
five, two. Okay. So I'm going to use integers here.
You could use float numbers, but it's going to
work also with integers. And then I can do print.
Let's just print the results here. Compute list
average from number list. And I can run the program,
run compute average from list. And you can see
three point five. So one thing to note here is
that I use number list here and number list here.
But as you can see, this is a different scope. This
is a global variable I use here. This is a local
variable I use inside the function. Note also that
I didn't cast the result, which is here a float
number. Okay. I didn't cast the result as a string,
but it's still working with the print function.
Why is that? Because if you just pass one
parameter, okay, if you don't concatenate with
other strings, you can just pass an integer or
float and it's going to work. Okay. And now the
exercise number four, I'm going to create
another Python file named Celsius to Fahrenheit.
And I'm going to create def Celsius to Fahrenheit.
Okay. And the parameter will be actually.
Celsius degrees. Okay. So this is a quite common
name to give when you have a function that converts
something into something else. You can start with
what you want to convert and then you add two,
the two keywords, and then the result you want
to get. So Celsius to Fahrenheit or kilometers to
meters, miles, two inches, whatever. Okay. And
of course I need to pass the degrees I want to
convert. So the Celsius degrees. And we are going
to apply the formula. Okay. And I can just do also
here return Celsius degrees multiplied by 1.8
plus 32. And this is going to give the Fahrenheit
degrees. So now in the main, I can do print, let's
say 20 degrees Celsius is equal to, and then plus
str, I'm going to make sure that I cast the
result as a string, Celsius to Fahrenheit of 20.
And I'm going to add another plus to put degrees
Fahrenheit. Okay. Let's see how it works. So run
Celsius to Fahrenheit. We have a problem here.
Because as you can see here, I opened the
parentheses here for string. I need to close it
here. And we have one parentheses that is not
correctly placed here. So make sure, as you can
see, that is a common source of errors. Okay.
Make sure that the parentheses are in the right
order. And you can see here, it's quite easy to
see with PyCharm. Okay. So I run this. And you can
see that when it is actually 68 Fahrenheit degrees,
it is 20 degrees Celsius. Okay. If you want, you
can just copy this multiple times. And let's say
you want to check 25. Here, 25. You can do this. Okay. 25 degrees. You get 75 degrees.
All right. And that is the end of the level two of this course.