1 00:00:00,000 --> 00:00:02,970 This is the solution for the exercises I gave you 2 00:00:02,970 --> 00:00:06,000 in the previous lesson, okay to practice with 3 00:00:06,000 --> 00:00:08,310 loops and conditions. So let's start with the 4 00:00:08,310 --> 00:00:12,360 first one. I'm going to right click here in the 5 00:00:12,360 --> 00:00:14,610 project New, Python file, 6 00:00:14,940 --> 00:00:20,244 let's name it max_value, okay, max_value.py, 7 00:00:20,244 --> 00:00:21,930 and I'm going to create a function to 8 00:00:21,930 --> 00:00:25,470 compute the max value inside the list. So def, 9 00:00:26,100 --> 00:00:34,230 compute_max_value_from_list. Okay, the name is a 10 00:00:34,230 --> 00:00:36,720 little bit long here, but it's quite explicit. And 11 00:00:36,720 --> 00:00:40,860 then I have number_list as a parameter, okay. So 12 00:00:40,860 --> 00:00:42,930 I have the name of the function, the parameters. 13 00:00:42,930 --> 00:00:45,870 Now I also know I will need to return something, 14 00:00:45,900 --> 00:00:48,660 okay. So at the end, I will have return 15 00:00:48,750 --> 00:00:52,470 something, I'm going to create first variable 16 00:00:52,470 --> 00:00:56,490 named max_value, okay, which I'm going to 17 00:00:56,490 --> 00:01:00,780 initialize to zero, okay, and return that at the 18 00:01:00,780 --> 00:01:04,530 end. Now, what I'm going to do is I'm going to go 19 00:01:04,530 --> 00:01:07,440 through all of the elements of the list, and I'm 20 00:01:07,440 --> 00:01:10,860 going to check that against the max_value. Okay, 21 00:01:10,980 --> 00:01:14,370 if one element is greater than max_value, I'm 22 00:01:14,370 --> 00:01:17,190 simply going to update the max_value, okay with 23 00:01:17,190 --> 00:01:20,850 the new element. So at the end, I will have the 24 00:01:20,910 --> 00:01:24,060 max_value inside this variable, and I can return 25 00:01:24,060 --> 00:01:29,610 it. Okay, so let's use a for loop. for number in 26 00:01:30,510 --> 00:01:35,160 number_list. For each number, I'm going to check 27 00:01:35,220 --> 00:01:43,353 if number is greater strictly greater than the max_value, 28 00:01:43,353 --> 00:01:48,120 in this case, I'm going to do max_value is 29 00:01:48,120 --> 00:01:54,069 equal to number. And well, that's pretty much it. 30 00:01:54,420 --> 00:01:57,990 So for each element, if the element is greater than 31 00:01:58,020 --> 00:02:02,220 any value we had before, we update, so we set the 32 00:02:02,220 --> 00:02:05,700 number, the actual value that is in the number 33 00:02:05,700 --> 00:02:09,479 variable right now, to the max_value variable, here, 34 00:02:09,660 --> 00:02:12,090 so at the end, we'll return the max_value. And 35 00:02:12,090 --> 00:02:14,070 now that we have the function, I'm going to create 36 00:02:14,070 --> 00:02:17,820 a list. So let's say number_list, I can use the 37 00:02:17,820 --> 00:02:20,310 same here, because this is a different scope. 38 00:02:21,210 --> 00:02:24,450 Let's use integers. But you can do with float 39 00:02:24,450 --> 00:02:30,660 numbers if you want, 3, 2, -3, 9, 40 00:02:30,750 --> 00:02:37,020 and then 4. And then max_value is equal to 41 00:02:37,680 --> 00:02:41,910 compute_max_value_from_list, I use the auto 42 00:02:41,910 --> 00:02:45,420 completion and I pass the number_list. And then 43 00:02:45,420 --> 00:02:51,600 again, print max_value. Okay, let's run that. So 44 00:02:51,600 --> 00:02:57,494 actually, this is loops, I'm going to run max_value here. 45 00:02:57,494 --> 00:03:00,330 And you can see max_value is nine, 46 00:03:00,360 --> 00:03:02,820 okay, so you can make a nice print if you want to 47 00:03:02,820 --> 00:03:06,720 here, okay, and we have correctly solved that 48 00:03:06,840 --> 00:03:09,750 problem. Here, we have the max_value here, nine, 49 00:03:09,960 --> 00:03:14,340 okay. So if we go through the function here, the 50 00:03:14,340 --> 00:03:17,730 first number is going to be 3. So if 51 00:03:17,730 --> 00:03:20,790 3 is greater than zero, yes, we update, and 52 00:03:20,790 --> 00:03:24,270 then 2, 2 is not greater than 3, because now 53 00:03:24,270 --> 00:03:27,180 the max_value is 3, so we don't enter that if. 54 00:03:27,570 --> 00:03:30,600 The next time -3 doesn't work either for 55 00:03:30,600 --> 00:03:34,260 the if, and then we have 9. 9 is greater than 56 00:03:34,440 --> 00:03:38,040 3, which is the current max_value. So we 57 00:03:38,070 --> 00:03:41,190 update max_value to 9. And then 4, we 58 00:03:41,190 --> 00:03:44,370 don't update because 4 is lower than 9, and 59 00:03:44,370 --> 00:03:48,120 then we return 9. Okay, now let's go to the 60 00:03:48,120 --> 00:03:52,860 second exercise, I'm creating a new Python file 61 00:03:52,860 --> 00:03:58,350 named get_numbers_from_user, I'm going to put it 62 00:03:58,440 --> 00:04:02,430 _1, okay, so we have 2 after that. So we first 63 00:04:02,430 --> 00:04:06,030 need to ask the user to give five elements, okay, 64 00:04:06,120 --> 00:04:09,510 add them in a list, and then compute the average of 65 00:04:09,510 --> 00:04:12,480 that list. So I'm going to create a number_list 66 00:04:12,720 --> 00:04:17,310 here, which is empty. And then I'm going to do for 67 00:04:18,120 --> 00:04:22,440 i in range(5), so this is going to be executed 68 00:04:22,470 --> 00:04:27,780 five time number_list.append, I'm going to 69 00:04:27,810 --> 00:04:32,040 append a new number and this is going to be input, 70 00:04:33,870 --> 00:04:40,503 Give a number: and also I am going to cast this as an integer. 71 00:04:42,210 --> 00:04:46,170 Okay, you could use float also. Actually, let's 72 00:04:46,170 --> 00:04:50,040 use float that maybe better, so we have a bit of 73 00:04:50,040 --> 00:04:53,320 precision and we use float number in the number_list. 74 00:04:53,320 --> 00:04:56,430 So number_list.append(), and we add a new 75 00:04:56,430 --> 00:04:59,400 element which is float so we cast the result to 76 00:04:59,400 --> 00:05:03,810 float, from the inputs of what the user gives us, 77 00:05:03,870 --> 00:05:07,050 okay, so now we have the list. I'm going to go back 78 00:05:07,050 --> 00:05:11,100 to when new in addition. So zero in addition, and 79 00:05:11,190 --> 00:05:13,980 then again, compute the average from that list. 80 00:05:13,980 --> 00:05:16,080 And to compute the average, I'm going to go back 81 00:05:16,080 --> 00:05:20,490 to the compute_average here, this function that 82 00:05:20,490 --> 00:05:23,250 I'm going to copy, it's working. So I'm going to 83 00:05:23,250 --> 00:05:31,996 copy this inside this program, okay, so compute_list_average. 84 00:05:31,996 --> 00:05:34,380 And now what I can do, so I had 85 00:05:34,470 --> 00:05:36,900 prints compute_list_average, I'm going to do 86 00:05:36,900 --> 00:05:40,860 that also. So the list has the same name. Okay, 87 00:05:40,860 --> 00:05:43,860 number_list. So once I have all the numbers I 88 00:05:43,860 --> 00:05:46,980 need, I simply call compute_list_average to 89 00:05:46,980 --> 00:05:49,320 compute the average and return the average and 90 00:05:49,320 --> 00:05:53,880 then print it on the screen. Okay, let's run that. 91 00:05:53,880 --> 00:05:59,490 So Run, get_numbers_from_user_1. So Give a number: 92 00:05:59,760 --> 00:06:04,590 one, Give a number: 4.5, okay, let's say 3, 93 00:06:05,550 --> 00:06:09,570 8, and 4. And you can see the average of 94 00:06:09,600 --> 00:06:14,100 the numbers here is 4.1. Okay, so it asked me to 95 00:06:14,100 --> 00:06:16,980 give five numbers and then compute the average 96 00:06:16,980 --> 00:06:21,390 and print the average. Alright. And now let's do 97 00:06:21,450 --> 00:06:24,150 the third exercise. I'm going to create a new 98 00:06:24,780 --> 00:06:31,170 Python file, get_numbers_from_user, let's name it 99 00:06:31,200 --> 00:06:37,470 _2. So when we needed to get exactly five numbers, 100 00:06:37,650 --> 00:06:41,010 the for loop was the best option. Now we need, 101 00:06:41,040 --> 00:06:43,800 what we need is, we need to ask the user to give 102 00:06:43,800 --> 00:06:46,500 some numbers, and we are only going to stop when 103 00:06:46,500 --> 00:06:50,700 the user gives us zero. Okay. So in this case, 104 00:06:50,700 --> 00:06:53,070 you're going to see that we are going to use a 105 00:06:53,130 --> 00:06:56,580 while loop. So first, I'm going to create an empty 106 00:06:56,580 --> 00:07:01,320 list. Okay, initialize it to zero elements. And 107 00:07:01,320 --> 00:07:05,220 then well, I'm going first to create a Boolean 108 00:07:05,400 --> 00:07:12,420 variable. ask_user_for_number is equal to true, okay. 109 00:07:12,750 --> 00:07:17,880 And I'm going to do while ask_user_for_number, and 110 00:07:17,880 --> 00:07:21,960 while this is true, we're going to keep asking the 111 00:07:21,960 --> 00:07:25,650 user for numbers, okay. And when the user gives us 112 00:07:25,680 --> 00:07:29,520 zero, we are going to put that flag to false. And 113 00:07:29,520 --> 00:07:31,740 then we are not going to enter the while anyway. 114 00:07:31,770 --> 00:07:34,740 Okay, so this is quite common practice to 115 00:07:34,740 --> 00:07:38,070 create a Boolean flag or Boolean variable. And 116 00:07:38,070 --> 00:07:41,670 then in the while, you check some conditions, and 117 00:07:41,670 --> 00:07:44,850 if some condition is met, you set that to false so 118 00:07:44,850 --> 00:07:47,310 the next time, you don't enter while. So I'm 119 00:07:47,310 --> 00:07:51,030 going to do user_input, I'm first going to get the 120 00:07:51,060 --> 00:07:53,880 user_input, okay, I'm going to cast it as a float 121 00:07:54,450 --> 00:07:59,580 input, Choose a number:. All right, and then I'm 122 00:07:59,580 --> 00:08:05,370 going to check if user_input is equal to 0.0, 123 00:08:05,370 --> 00:08:08,400 because this is a float. What we do is we set the 124 00:08:08,430 --> 00:08:15,270 ask_user_for_numbers flag to false, okay. And then else, 125 00:08:15,900 --> 00:08:18,810 I'm coming back to this indentation. So here you can 126 00:08:18,810 --> 00:08:22,350 see the if is inside the while. So we have four 127 00:08:22,380 --> 00:08:25,800 indentation, and then for this, we have four more 128 00:08:25,830 --> 00:08:29,970 indentation. So we have eight spaces total. And 129 00:08:29,970 --> 00:08:32,669 then I write the else, I need to write else at the 130 00:08:32,669 --> 00:08:36,870 same indentation as the if, okay, but still with 131 00:08:36,900 --> 00:08:39,510 four spaces because we are in the while. Okay, 132 00:08:39,960 --> 00:08:43,473 else, I'm going to do a number_list.append 133 00:08:43,473 --> 00:08:49,050 with the user_input. Okay, so what is it 134 00:08:49,050 --> 00:08:51,750 going to do? First, this is going to be true 135 00:08:51,750 --> 00:08:55,920 because this is tricky. We ask the user for a 136 00:08:55,920 --> 00:08:58,800 number that we cast as a float, and then we 137 00:08:58,800 --> 00:09:01,860 check if this is equal to zero. So if this is not 138 00:09:01,860 --> 00:09:05,490 zero, we enter the else, we add the number to the 139 00:09:05,490 --> 00:09:08,730 list, we come back to while, and we continue because 140 00:09:08,730 --> 00:09:12,450 this is true. Okay. Now when the user gives the 141 00:09:12,450 --> 00:09:16,170 number zero, this is going to be true. So what we 142 00:09:16,170 --> 00:09:21,000 do is we set this Boolean variable to false, we 143 00:09:21,000 --> 00:09:23,730 don't enter the else okay, because this is true. 144 00:09:24,090 --> 00:09:27,420 We come back to while, this is false. And so 145 00:09:27,420 --> 00:09:29,430 basically, we don't enter this block of code, then 146 00:09:29,430 --> 00:09:33,000 we continue the execution of the code here with 147 00:09:33,000 --> 00:09:36,390 zero indentation. And now what I can do is I can 148 00:09:36,480 --> 00:09:40,200 compute the average so I'm going simply to get 149 00:09:40,260 --> 00:09:43,530 this function again. Put it there. 150 00:09:45,240 --> 00:09:50,040 And print compute_list_average. Let's use what 151 00:09:50,040 --> 00:09:54,720 we have written before and print like this. Okay, 152 00:09:54,750 --> 00:10:00,232 let's run that. So actually Run, get_numbers_from_user_2, 153 00:10:00,232 --> 00:10:03,480 Choose a number:, I'm going to give two. Choose 154 00:10:03,480 --> 00:10:08,670 a number: 5, 2, let's say -1, you can 155 00:10:08,670 --> 00:10:11,520 see I can continue to give any number I want, 156 00:10:11,520 --> 00:10:15,930 okay? And then I'm going to give zero. And as you 157 00:10:15,930 --> 00:10:19,830 can see, once I give zero, this stops and this 158 00:10:19,830 --> 00:10:23,490 prints the average that was computed from all 159 00:10:23,490 --> 00:10:29,760 the elements here I have given previously. And as 160 00:10:29,760 --> 00:10:33,210 an improvement here, maybe that you could also 161 00:10:33,330 --> 00:10:36,900 create a function to do that, okay, and function 162 00:10:37,200 --> 00:10:41,370 to do that, okay. I'll let you think of how you can 163 00:10:41,370 --> 00:10:44,190 name the function if you need to give parameters 164 00:10:44,190 --> 00:10:48,120 or not, etc. That's an additional practice. And 165 00:10:48,120 --> 00:10:50,490 we'll alright, you have just finished delivering 166 00:10:50,500 --> 00:10:53,100 three of these Python codes.