1
00:00:00,750 --> 00:00:03,060
All right. So in today's coding challenge,

2
00:00:03,090 --> 00:00:05,250
we're going to do a little bit of wall painting.

3
00:00:05,850 --> 00:00:10,850
And the idea is we're going to create a program that helps us calculate how many

4
00:00:11,220 --> 00:00:16,220
cans of paint we need to buy for a given surface area of wall.

5
00:00:17,220 --> 00:00:19,320
The maths involved is very simple

6
00:00:19,350 --> 00:00:22,110
and I'm going to talk you through it if you're not confident with maths.

7
00:00:22,500 --> 00:00:26,370
But essentially we want it to be able to use our inputs

8
00:00:26,640 --> 00:00:31,380
to modify our function so that it does something every single time we use it.

9
00:00:32,430 --> 00:00:37,410
Go ahead and click on day 8.1 area calculation,

10
00:00:38,040 --> 00:00:42,510
and we're going to complete this coding challenge. So firstly,

11
00:00:42,510 --> 00:00:46,290
you'll notice that down here on lines, 10, 11, 12, and 13,

12
00:00:46,710 --> 00:00:50,160
I've got some code that's going to help us check your solution.

13
00:00:50,700 --> 00:00:55,700
And we're going to use your function to actually calculate the number of cans of

14
00:00:56,820 --> 00:00:57,810
paint we need to buy.

15
00:00:58,470 --> 00:01:02,970
You're basically painting a wall and the instructions on the paint cans says that

16
00:01:02,970 --> 00:01:03,803
one can

17
00:01:04,110 --> 00:01:09,110
can cover five square meters of wall. Given any height and any width, your

18
00:01:10,500 --> 00:01:15,360
program needs to calculate how many cans of paint you need to buy. The maths,

19
00:01:15,570 --> 00:01:17,040
as I say, is pretty simple.

20
00:01:17,070 --> 00:01:21,150
All you have to do is multiply the height by the width to get the area of the

21
00:01:21,150 --> 00:01:25,290
wall, and then you simply divided by the coverage per can.

22
00:01:25,860 --> 00:01:28,340
We said that five square meters per can, so

23
00:01:28,350 --> 00:01:30,750
it means you are always going to be dividing by five.

24
00:01:31,230 --> 00:01:35,250
So let's say that we had a wall which was two meters high and four meters wide,

25
00:01:35,700 --> 00:01:39,150
and the coverage, as always, is going to be equal to five.

26
00:01:40,020 --> 00:01:43,800
Then the calculation would be a simple as 2 x 4 divided by 5,

27
00:01:43,830 --> 00:01:48,210
which is 1.6. So we need 1.6 cans of paint,

28
00:01:48,540 --> 00:01:52,050
but because you can't buy 0.6 can of a paint,

29
00:01:52,470 --> 00:01:56,460
I've tried this in the store. They said no and they told me to go away.

30
00:01:57,300 --> 00:02:02,300
So we actually have to round up to the nearest whole number.

31
00:02:03,240 --> 00:02:06,870
So in this case, we need to buy two cans. Now,

32
00:02:06,900 --> 00:02:10,830
the really important thing to draw your attention to is down here

33
00:02:10,860 --> 00:02:13,350
I actually call the function.

34
00:02:13,770 --> 00:02:18,750
So I'm calling the function and I'm giving in some named arguments.

35
00:02:19,080 --> 00:02:23,520
So this is really, really important. If you review the previous lessons,

36
00:02:23,580 --> 00:02:27,870
then you should know what name you need to give your function and what

37
00:02:27,870 --> 00:02:32,870
name you need to give the parameters for the code to actually work without any

38
00:02:32,970 --> 00:02:37,950
errors. Remember to take a look at the example inputs down here

39
00:02:37,980 --> 00:02:42,980
and example output because you need to print out something along these lines.

40
00:02:43,650 --> 00:02:48,300
So it should say you'll need X cans of paint depending on your calculation.

41
00:02:48,870 --> 00:02:52,920
And finally, if you need a hint as to how to round up a number,

42
00:02:53,250 --> 00:02:57,180
then you can either go ahead and Google for it, use Stack Overflow,

43
00:02:57,240 --> 00:03:01,540
that's probably the best way, or you could give it a guess. And finally,

44
00:03:01,540 --> 00:03:02,680
if you really get stuck,

45
00:03:02,710 --> 00:03:06,640
take a look at the hint section. And once you're done,

46
00:03:06,700 --> 00:03:10,060
then come back over here and we'll walk through the solution together.

47
00:03:10,600 --> 00:03:14,080
Pause the video now and go ahead and complete this coding exercise.

48
00:03:21,750 --> 00:03:22,380
Yeah.

49
00:03:22,380 --> 00:03:25,560
All right. So, did you manage to get that right? If not,

50
00:03:25,710 --> 00:03:28,410
take a look at this video and then once you're done,

51
00:03:28,620 --> 00:03:33,090
take a look at the solution code and try to compare against your code to see if

52
00:03:33,090 --> 00:03:37,110
there were any errors that you made. Now, this challenge is pretty doable.

53
00:03:37,470 --> 00:03:42,470
All we have to do is to create our function and we create any function using def.

54
00:03:43,500 --> 00:03:46,830
And how do we know what name to give this function? Well,

55
00:03:46,830 --> 00:03:49,950
we take a look at where the function was called down here,

56
00:03:50,520 --> 00:03:53,850
and you can see that it's called paint_calc.

57
00:03:54,240 --> 00:03:56,910
So this is what we have to give our function as the name.

58
00:03:57,630 --> 00:04:02,370
And then we of course have our parentheses, our colon, and then inside here,

59
00:04:02,400 --> 00:04:04,920
we're going to do all the calculations. Now,

60
00:04:04,920 --> 00:04:09,030
the next thing we have to look at is what name we should give our parameters.

61
00:04:09,510 --> 00:04:12,540
And you can see I've got named arguments here.

62
00:04:12,930 --> 00:04:17,820
So that means each of these are going to be the names of the parameters; height,

63
00:04:18,300 --> 00:04:19,950
width, and cover.

64
00:04:20,460 --> 00:04:25,460
So the parameters are going to go inside the parentheses height, width, and cover.

65
00:04:27,060 --> 00:04:31,620
And it's only if we actually spell these parameters exactly the same as what we've

66
00:04:31,620 --> 00:04:35,280
got down here in the arguments will our code actually work.

67
00:04:36,780 --> 00:04:41,160
Now let's go ahead and take the height and multiply it by the width to get the

68
00:04:41,160 --> 00:04:45,240
area. So area = height * width.

69
00:04:46,440 --> 00:04:50,460
And then with that area, we can divide it by the coverage per can.

70
00:04:50,520 --> 00:04:55,520
So let's call it num_of_cans = area

71
00:04:55,890 --> 00:04:57,720
/ cover.

72
00:05:00,000 --> 00:05:04,560
And now remember that this is going to be a decimal number occasionally like

73
00:05:04,560 --> 00:05:05,393
here,

74
00:05:05,520 --> 00:05:09,780
and we have two round it up to the next whole number.

75
00:05:10,410 --> 00:05:14,370
If we take a look at this particular Stack Overflow,

76
00:05:15,480 --> 00:05:18,540
you can see that the question is exactly what we want, right?

77
00:05:18,540 --> 00:05:22,620
How do you round up a number in Python? And if you did some Googling,

78
00:05:22,620 --> 00:05:26,730
then this might have been the Stack Overflow question you would have landed on.

79
00:05:27,360 --> 00:05:32,220
And essentially it tells us you have to import maths, and from the math module,

80
00:05:32,230 --> 00:05:36,180
you use a function called ceiling. And then inside the parentheses,

81
00:05:36,270 --> 00:05:39,390
you put in the inputs. To do that,

82
00:05:39,420 --> 00:05:41,280
we first import math.

83
00:05:42,690 --> 00:05:47,690
It's math without the 's.' And then we can call it a math.ceil and wrap this

84
00:05:50,130 --> 00:05:52,110
result around the parentheses.

85
00:05:52,410 --> 00:05:56,250
So this is going to take the result here from a decimal number to

86
00:05:56,250 --> 00:05:59,510
a whole number by rounding it up to the ceiling.

87
00:06:00,260 --> 00:06:03,380
So now we're finally ready to give our output,

88
00:06:03,410 --> 00:06:05,660
so we're going to do that by printing it.

89
00:06:05,660 --> 00:06:09,560
So we're going to print and I'm definitely going to need an f-string.

90
00:06:09,560 --> 00:06:13,040
So I'm going to put an F in there and paste this string.

91
00:06:13,340 --> 00:06:16,430
And instead of having the hard-coded five here,

92
00:06:16,940 --> 00:06:20,630
I'm going to put in the num_of_cans instead.

93
00:06:21,500 --> 00:06:26,500
So now when this line of code on line 17 calls

94
00:06:27,050 --> 00:06:31,310
paint_calc passing in each of these arguments,

95
00:06:32,210 --> 00:06:37,210
then it's going to be able to run this function here and print out how many cans

96
00:06:38,180 --> 00:06:42,830
of paint I'll need. Now, all we have to do is test our code.

97
00:06:43,010 --> 00:06:46,490
So let's hit run and let's put in a random height.

98
00:06:46,490 --> 00:06:50,930
Let's say it's five meters high and three meters wide.

99
00:06:51,560 --> 00:06:54,050
And it tells us that we need three cans of paint.

100
00:06:54,560 --> 00:06:57,890
So five by three,

101
00:06:57,890 --> 00:06:59,750
the area is 15 square meters,

102
00:07:00,140 --> 00:07:03,860
15 divided by five is three. Now,

103
00:07:03,860 --> 00:07:05,960
what if we had a decimal number?

104
00:07:06,290 --> 00:07:10,430
Let's say we had a height of 7 and a width of 13.

105
00:07:11,180 --> 00:07:14,060
Then in this case, it tells us we need 19 cans.

106
00:07:14,120 --> 00:07:19,120
So 7 times 13 is 91. 91 divided by 5 is 18.2 and 18.2

107
00:07:22,010 --> 00:07:23,900
round it up is 19.

108
00:07:24,680 --> 00:07:29,330
And we've now completed this challenge. Did you manage to get it right?

109
00:07:29,660 --> 00:07:34,660
If not, be sure to watch this video and see if you can go back and fix your code

110
00:07:35,150 --> 00:07:39,560
and you can compare it against the solution code I've got here by going to this

111
00:07:39,590 --> 00:07:40,580
link right here.

112
00:07:42,260 --> 00:07:46,220
And once you're done, head over to the next lesson where I've got a slightly

113
00:07:46,220 --> 00:07:48,170
harder coding challenge for you.

