1 00:00:00,000 --> 00:00:05,000 This is the solution of the exercises for the level 2 of this course. 2 00:00:05,001 --> 00:00:08,020 So the first exercise is actually here. You have 3 00:00:08,032 --> 00:00:11,000 to ask for the name and age of a user and print 4 00:00:11,001 --> 00:00:13,920 the info. So basically that's the function. 5 00:00:13,932 --> 00:00:17,000 So I'm not going to rewrite it one more time. 6 00:00:17,001 --> 00:00:20,054 What I advised you to do was to actually try to 7 00:00:20,066 --> 00:00:23,000 rewrite it by yourself and see if you can get 8 00:00:23,001 --> 00:00:26,448 this also. Okay. Now I'm going to go to the 9 00:00:26,460 --> 00:00:30,000 second exercise which is to ask the user for 10 00:00:30,001 --> 00:00:33,085 two integer numbers, add them, print the result. 11 00:00:33,097 --> 00:00:36,000 So I'm going to create a new Python file here 12 00:00:36,001 --> 00:00:41,550 and let's say add to it. I'm going to name it 13 00:00:41,562 --> 00:00:47,000 add to it. So I'm going to create a function 14 00:00:47,001 --> 00:00:51,756 which is going to add two numbers. So add 15 00:00:51,768 --> 00:00:57,000 two numbers. Okay. You can use numbers inside 16 00:00:57,001 --> 00:01:00,989 function names. Just make sure you don't start 17 00:01:01,001 --> 00:01:05,000 with a number. And well, for each word you add 18 00:01:05,001 --> 00:01:08,180 an underscore. And then I will need two parameters 19 00:01:08,192 --> 00:01:11,000 here. I will need number one and number two. 20 00:01:11,001 --> 00:01:14,898 So I can use A and B here. So I told you to 21 00:01:14,910 --> 00:01:19,000 use meaningful names and maybe avoid A and B. 22 00:01:19,001 --> 00:01:21,925 But if you just need to add two numbers, using 23 00:01:21,937 --> 00:01:25,000 A and B actually makes much more sense. Then I'm 24 00:01:25,001 --> 00:01:28,380 just going to do return A plus B. Okay. This is 25 00:01:28,392 --> 00:01:32,000 very easy to read. So this function is quite easy. 26 00:01:32,001 --> 00:01:35,560 We just receive two parameters. We return the same 27 00:01:35,572 --> 00:01:39,000 of those parameters. Now I'm going to go back to 28 00:01:39,001 --> 00:01:46,214 the normal invitation and I'm going to do input. 29 00:01:46,226 --> 00:01:53,000 Enter number one. Okay. And then input. Enter 30 00:01:53,001 --> 00:01:56,376 number two. In order to keep this and be able 31 00:01:56,388 --> 00:02:00,000 to do something with that, I'm going to save it. 32 00:02:00,001 --> 00:02:04,074 So number one is equal to input. And then number 33 00:02:04,086 --> 00:02:08,000 two is equal to input. Okay. I'm going to save 34 00:02:08,001 --> 00:02:11,898 those into variables. And because the input 35 00:02:11,910 --> 00:02:16,000 is returning a string, I need to cast it with 36 00:02:16,001 --> 00:02:20,261 int like this. Okay. Int number one. Int number 37 00:02:20,273 --> 00:02:24,000 two. I have my number one, my number two. 38 00:02:24,001 --> 00:02:27,565 Now what I can do, I can do print. So I'm going 39 00:02:27,577 --> 00:02:31,000 to do all that in one line. And let's say str 40 00:02:31,001 --> 00:02:39,000 number one. So now that I have casted number one, so the input into the integer, 41 00:02:39,001 --> 00:02:43,028 if I want to print it, I need to cast it again to a 42 00:02:43,040 --> 00:02:47,000 string. Okay. So number one plus and then the plus 43 00:02:47,001 --> 00:02:54,406 sign plus str number two plus and then equal, 44 00:02:54,418 --> 00:03:02,000 then plus. And I can call the function add two 45 00:03:02,001 --> 00:03:04,956 numbers. I can use the autocompletion. And I'm 46 00:03:04,968 --> 00:03:08,000 going to give number one and number two. As you 47 00:03:08,001 --> 00:03:12,539 can see, the line starts to be quite long. So 48 00:03:12,551 --> 00:03:17,000 what I can do, I can after here, after this, 49 00:03:17,001 --> 00:03:19,958 I can come back to a new line. And this is going 50 00:03:19,970 --> 00:03:23,000 to work. So make sure you have the correct amount 51 00:03:23,001 --> 00:03:26,378 of parentheses. And if you have an instruction 52 00:03:26,390 --> 00:03:30,000 that is quite long, well, you can cut this. Okay. 53 00:03:30,001 --> 00:03:31,947 So if you have a plus, for example, you can cut 54 00:03:31,959 --> 00:03:34,000 at the plus, just go back to a new line with some 55 00:03:34,001 --> 00:03:37,378 indentation. Okay. It's going to automatically 56 00:03:37,390 --> 00:03:41,000 indent this. Actually, the number of indentations 57 00:03:41,001 --> 00:03:44,340 here is not important. Okay. Or if you have a 58 00:03:44,352 --> 00:03:48,000 comma, this is also a good place to cut the line. 59 00:03:48,001 --> 00:03:54,000 If you have a comma, you can just go back to a new line. It's going to work also. 60 00:03:54,001 --> 00:03:59,100 So now I'm going to run that, actually add two 61 00:03:59,112 --> 00:04:04,000 ints. So I need to run the dot add two ints. 62 00:04:04,001 --> 00:04:08,668 And turn number one, two, and turn number two, five. 63 00:04:08,680 --> 00:04:13,000 And we have an error because I also need to cast 64 00:04:13,001 --> 00:04:15,489 the results as a string. Okay. The result is an 65 00:04:15,501 --> 00:04:18,000 integer. I need to cast the result as a string. 66 00:04:18,001 --> 00:04:21,946 I run add two ints. So enter number one, three, 67 00:04:21,958 --> 00:04:26,000 number two, four. And we can see three plus four 68 00:04:26,001 --> 00:04:29,825 is equal to seven. So that's the program. Okay. 69 00:04:29,837 --> 00:04:34,000 Now let's go to the number three. We have to create 70 00:04:34,001 --> 00:04:37,818 a list of float numbers, compute the average, 71 00:04:37,830 --> 00:04:42,000 and print the average. So again, new Python file. 72 00:04:42,001 --> 00:04:47,427 Let's say compute average from list. And I'm 73 00:04:47,439 --> 00:04:53,000 going to create def compute. So let's name it 74 00:04:53,001 --> 00:04:57,720 compute list average. Okay. And number list. 75 00:04:57,732 --> 00:05:03,000 Okay. So one function which is named compute list 76 00:05:03,001 --> 00:05:07,989 average with one parameter, which is the list. 77 00:05:08,001 --> 00:05:13,000 And I can do return sum number list divided by 78 00:05:13,001 --> 00:05:16,989 length of number list. Okay. I compute the sum. 79 00:05:17,001 --> 00:05:21,000 I divide by the length. And this is a function. 80 00:05:21,001 --> 00:05:25,827 So now I can use this for any list I have. So 81 00:05:25,839 --> 00:05:31,000 number list is equal to, let's say, three, four, 82 00:05:31,001 --> 00:05:34,669 five, two. Okay. So I'm going to use integers here. 83 00:05:34,681 --> 00:05:38,000 You could use float numbers, but it's going to 84 00:05:38,001 --> 00:05:42,072 work also with integers. And then I can do print. 85 00:05:42,084 --> 00:05:46,000 Let's just print the results here. Compute list 86 00:05:46,001 --> 00:05:51,295 average from number list. And I can run the program, 87 00:05:51,307 --> 00:05:56,000 run compute average from list. And you can see 88 00:05:56,001 --> 00:05:58,894 three point five. So one thing to note here is 89 00:05:58,906 --> 00:06:02,000 that I use number list here and number list here. 90 00:06:02,001 --> 00:06:05,079 But as you can see, this is a different scope. This 91 00:06:05,091 --> 00:06:08,000 is a global variable I use here. This is a local 92 00:06:08,001 --> 00:06:12,112 variable I use inside the function. Note also that 93 00:06:12,124 --> 00:06:16,000 I didn't cast the result, which is here a float 94 00:06:16,001 --> 00:06:19,111 number. Okay. I didn't cast the result as a string, 95 00:06:19,123 --> 00:06:22,000 but it's still working with the print function. 96 00:06:22,001 --> 00:06:24,816 Why is that? Because if you just pass one 97 00:06:24,828 --> 00:06:28,000 parameter, okay, if you don't concatenate with 98 00:06:28,001 --> 00:06:30,956 other strings, you can just pass an integer or 99 00:06:30,968 --> 00:06:34,000 float and it's going to work. Okay. And now the 100 00:06:34,001 --> 00:06:39,056 exercise number four, I'm going to create 101 00:06:39,068 --> 00:06:45,000 another Python file named Celsius to Fahrenheit. 102 00:06:45,001 --> 00:06:53,780 And I'm going to create def Celsius to Fahrenheit. 103 00:06:53,792 --> 00:07:01,000 Okay. And the parameter will be actually. 104 00:07:02,000 --> 00:07:06,836 Celsius degrees. Okay. So this is a quite common 105 00:07:06,848 --> 00:07:12,000 name to give when you have a function that converts 106 00:07:12,001 --> 00:07:15,083 something into something else. You can start with 107 00:07:15,095 --> 00:07:18,000 what you want to convert and then you add two, 108 00:07:18,001 --> 00:07:22,346 the two keywords, and then the result you want 109 00:07:22,358 --> 00:07:27,000 to get. So Celsius to Fahrenheit or kilometers to 110 00:07:27,001 --> 00:07:30,489 meters, miles, two inches, whatever. Okay. And 111 00:07:30,501 --> 00:07:34,000 of course I need to pass the degrees I want to 112 00:07:34,001 --> 00:07:37,948 convert. So the Celsius degrees. And we are going 113 00:07:37,960 --> 00:07:42,000 to apply the formula. Okay. And I can just do also 114 00:07:42,001 --> 00:07:47,254 here return Celsius degrees multiplied by 1.8 115 00:07:47,266 --> 00:07:53,000 plus 32. And this is going to give the Fahrenheit 116 00:07:53,001 --> 00:07:58,544 degrees. So now in the main, I can do print, let's 117 00:07:58,556 --> 00:08:04,000 say 20 degrees Celsius is equal to, and then plus 118 00:08:04,001 --> 00:08:08,714 str, I'm going to make sure that I cast the 119 00:08:08,726 --> 00:08:14,000 result as a string, Celsius to Fahrenheit of 20. 120 00:08:16,000 --> 00:08:21,488 And I'm going to add another plus to put degrees 121 00:08:21,500 --> 00:08:27,000 Fahrenheit. Okay. Let's see how it works. So run 122 00:08:27,001 --> 00:08:32,276 Celsius to Fahrenheit. We have a problem here. 123 00:08:32,288 --> 00:08:37,000 Because as you can see here, I opened the 124 00:08:37,001 --> 00:08:40,565 parentheses here for string. I need to close it 125 00:08:40,577 --> 00:08:44,000 here. And we have one parentheses that is not 126 00:08:44,001 --> 00:08:47,054 correctly placed here. So make sure, as you can 127 00:08:47,066 --> 00:08:50,000 see, that is a common source of errors. Okay. 128 00:08:50,001 --> 00:08:52,989 Make sure that the parentheses are in the right 129 00:08:53,001 --> 00:08:56,000 order. And you can see here, it's quite easy to 130 00:08:56,001 --> 00:08:59,949 see with PyCharm. Okay. So I run this. And you can 131 00:08:59,961 --> 00:09:04,000 see that when it is actually 68 Fahrenheit degrees, 132 00:09:04,001 --> 00:09:07,989 it is 20 degrees Celsius. Okay. If you want, you 133 00:09:08,001 --> 00:09:12,000 can just copy this multiple times. And let's say 134 00:09:12,001 --> 00:09:24,000 you want to check 25. Here, 25. You can do this. Okay. 25 degrees. You get 75 degrees. 135 00:09:24,001 --> 00:09:37,000 All right. And that is the end of the level two of this course.