1
00:00:00,700 --> 00:00:03,490
Hi, and welcome to today's bonus example.

2
00:00:03,490 --> 00:00:09,760
In yesterday's bonus example, we worked on this code, so if you haven't seen that video yet, please

3
00:00:09,760 --> 00:00:12,760
do so because that video is related to this one.

4
00:00:12,760 --> 00:00:18,220
In a nutshell, what we did is we asked the user to enter Ft and inches, so we got that as a string

5
00:00:18,220 --> 00:00:18,460
here.

6
00:00:18,460 --> 00:00:22,570
And then this convert function extracted the numbers from that string.

7
00:00:23,290 --> 00:00:30,280
So if the user entered 312, for example, we extracted three and 12 and we convert them into floats

8
00:00:30,280 --> 00:00:35,320
here and there and we calculated meters out of those two numbers.

9
00:00:35,320 --> 00:00:40,630
So three and 12, we multiply that by that number and that's by that and we got meters.

10
00:00:40,630 --> 00:00:48,730
So this was good to return a number instead of a string where you had meters inside the string and you

11
00:00:48,730 --> 00:00:51,400
had some other information here text.

12
00:00:51,610 --> 00:00:59,890
So that was not good because we couldn't use that output in other parts of the codes as we do here.

13
00:00:59,890 --> 00:01:01,210
So here we want to.

14
00:01:01,890 --> 00:01:05,489
Compare that meters with some other value.

15
00:01:05,610 --> 00:01:08,460
So it's good to decouple your output.

16
00:01:08,490 --> 00:01:18,330
The problem this brought about was that now what if we want to print out the feed value that the user

17
00:01:18,330 --> 00:01:20,160
entered so feeds?

18
00:01:20,160 --> 00:01:26,070
We cannot access that value from that function because that feed is a local variable.

19
00:01:26,070 --> 00:01:37,920
So we cannot say that and same four inches is equal to we can get meters because meters is the output.

20
00:01:37,920 --> 00:01:44,340
So when we return a variable, a local variable, that variable can be accessed through the function

21
00:01:44,340 --> 00:01:45,000
call.

22
00:01:45,000 --> 00:01:46,350
So that's okay.

23
00:01:46,350 --> 00:01:46,980
We can.

24
00:01:47,890 --> 00:01:49,030
Access results.

25
00:01:49,030 --> 00:01:50,890
But how do we access FT and inches?

26
00:01:50,920 --> 00:01:55,360
Well, the problem lies again in decoupling.

27
00:01:55,390 --> 00:01:58,390
Today, we're going to talk about decoupling functions.

28
00:01:58,420 --> 00:02:00,580
Yesterday, we talked about decoupling output.

29
00:02:00,610 --> 00:02:03,070
Now, this function is doing too much.

30
00:02:03,100 --> 00:02:04,360
It's doing two things.

31
00:02:04,360 --> 00:02:07,750
First, it is passing that string.

32
00:02:07,750 --> 00:02:12,640
So the user enters FT and inches and the function is splitting it.

33
00:02:12,640 --> 00:02:20,520
And then it's extracting numbers from that string and then it converts those to numbers into meters.

34
00:02:20,530 --> 00:02:23,490
However, the name of the function is convert.

35
00:02:23,500 --> 00:02:29,160
So this function is doing too much functions should do one thing and do it well.

36
00:02:29,170 --> 00:02:31,390
So the solution here is decoupling.

37
00:02:31,390 --> 00:02:35,410
And the way we do decoupling here is by creating two different functions.

38
00:02:36,310 --> 00:02:41,320
Let's call this pass or extract whatever we like.

39
00:02:41,470 --> 00:02:51,460
So pause and feed inches goes as the inputs of this pause function, and then this code goes in here.

40
00:02:51,760 --> 00:02:59,620
So we get ports, we say feed inches, split feed is converted into floats, interest is converted into

41
00:02:59,620 --> 00:03:00,700
float as well.

42
00:03:00,700 --> 00:03:03,370
And then we return those two numbers.

43
00:03:03,370 --> 00:03:10,840
So we make these two numbers basically available to be accessed from the other components of the script.

44
00:03:10,840 --> 00:03:13,510
You can see fit and inches.

45
00:03:13,510 --> 00:03:17,080
So if I demonstrate you what I'm doing here, this should work.

46
00:03:17,080 --> 00:03:21,400
Like this lets me paste this function here in the python console.

47
00:03:21,400 --> 00:03:30,520
So pause if we call it and we say 412 and we press enter, what we get in return is a tuple with these

48
00:03:30,520 --> 00:03:31,240
two values.

49
00:03:31,240 --> 00:03:38,440
So when you return to variables, they're what you get in return is not one value, but two values enclosed

50
00:03:38,440 --> 00:03:39,310
in a tuple.

51
00:03:39,310 --> 00:03:48,190
So if you check the type of that value, it's going to be a tuple you see or what you could do.

52
00:03:48,190 --> 00:03:52,960
If you want to improve that even further, you could use a dictionary.

53
00:03:53,020 --> 00:04:00,940
You could say feed is ft and inches as inches.

54
00:04:00,940 --> 00:04:04,780
You remember that with dictionaries we have pairs of keys and values.

55
00:04:04,780 --> 00:04:06,910
So the key here is the string.

56
00:04:06,910 --> 00:04:10,540
Its value is the number that the variable feeds contains.

57
00:04:10,540 --> 00:04:13,150
Then we have another pair of key and value.

58
00:04:13,150 --> 00:04:20,800
So if I get this functional, I define it in here in the console, execute it and then call it pause

59
00:04:20,950 --> 00:04:22,360
for 12.

60
00:04:22,450 --> 00:04:24,370
What we get is a dictionary.

61
00:04:24,370 --> 00:04:28,930
So now we have feeds and that is the value of feed and that's the value of inches.

62
00:04:28,930 --> 00:04:35,830
So that's a dictionary, which means you can access that value by providing here the name of its key

63
00:04:36,070 --> 00:04:41,650
feed as a string, and it gives you 4.0 and you can do the same four inches.

64
00:04:44,090 --> 00:04:45,080
12.0.

65
00:04:45,080 --> 00:04:53,990
So going back to the program in the F string here, we could say to get feet, we say pause.

66
00:04:54,020 --> 00:05:03,560
You call pause with feet and inches inside and then access feeds from that.

67
00:05:03,560 --> 00:05:08,420
Or even better, you could get that expression and do that outside.

68
00:05:08,420 --> 00:05:10,730
And you see past.

69
00:05:10,730 --> 00:05:11,900
Is that so?

70
00:05:11,900 --> 00:05:15,200
That should give you the dictionary as you saw here.

71
00:05:15,500 --> 00:05:17,360
So that gives the dictionary.

72
00:05:17,360 --> 00:05:17,960
Right.

73
00:05:18,890 --> 00:05:25,640
And then in here you see post and you extract feed.

74
00:05:25,670 --> 00:05:31,400
You need to use single quotes here since you have used double quotes outside the string.

75
00:05:31,400 --> 00:05:33,710
So for that string use single quotes.

76
00:05:33,710 --> 00:05:41,180
So again, that returns a dictionary, that dictionary and that extracts that feed value from the dictionary.

77
00:05:41,180 --> 00:05:47,090
Same, we do four interest post entries and that is equal to result.

78
00:05:47,090 --> 00:05:49,130
So result comes from convert.

79
00:05:49,130 --> 00:05:52,160
Now convert we need to work on this function as well.

80
00:05:52,550 --> 00:05:56,000
Convert now shouldn't be getting a string.

81
00:05:56,000 --> 00:06:00,770
It should get the units to convert, which is ft and inches.

82
00:06:00,770 --> 00:06:06,590
So it should get these as numbers and it shouldn't be doing any conversion from string to float.

83
00:06:06,590 --> 00:06:08,750
Just get numbers, return numbers.

84
00:06:08,750 --> 00:06:16,600
So here in convert then we say post feed to pass the feed value.

85
00:06:16,610 --> 00:06:19,400
The problem is this is highlighted in red.

86
00:06:19,400 --> 00:06:25,490
It says that this variable has not been defined, so this variable should be defined before it's used

87
00:06:25,490 --> 00:06:25,700
here.

88
00:06:25,700 --> 00:06:29,210
We are using it here, but we haven't defined it in this part.

89
00:06:29,210 --> 00:06:32,840
Therefore I just cut this from there and paste it up here.

90
00:06:32,840 --> 00:06:40,430
So we first get the feeds and inches and then we pass those feed and inches in here, converts again

91
00:06:41,180 --> 00:06:45,080
inches make a break line there, and that's the codes.

92
00:06:45,080 --> 00:06:47,030
Finally, let's run it.

93
00:06:47,210 --> 00:06:57,230
Feed on inches 311 and let's see, we've got an error bonus 13, line 17 which is in here.

94
00:06:57,350 --> 00:07:01,460
It says that function is not subscript double.

95
00:07:01,760 --> 00:07:02,060
Yeah.

96
00:07:02,060 --> 00:07:04,550
So here I did convert.

97
00:07:04,550 --> 00:07:06,200
I should have done post.

98
00:07:06,620 --> 00:07:07,610
Let's run again.

99
00:07:07,820 --> 00:07:16,850
310 Presenter Three feet and ten inches is equal to 116 kids can use the slide because the kid is higher

100
00:07:16,850 --> 00:07:17,990
than one metre.

101
00:07:18,670 --> 00:07:22,750
So that's how it should be working.

102
00:07:22,870 --> 00:07:23,920
That's a complete code.

103
00:07:23,920 --> 00:07:31,570
So again, to wrap it up, we get a string here and we want a function which does parsing of that string.

104
00:07:31,570 --> 00:07:34,060
We want to extract the numbers from that string.

105
00:07:34,060 --> 00:07:38,470
So it's better to have that parsing inside one single function.

106
00:07:38,470 --> 00:07:41,500
So functions that way are decoupled.

107
00:07:41,620 --> 00:07:43,480
That solves the problem.

108
00:07:43,480 --> 00:07:49,630
As I explained to you, that solves the problem of being able to access those things from that function.

109
00:07:49,630 --> 00:07:53,350
So we access those things and we use them in this f string here.

110
00:07:53,350 --> 00:07:58,600
And then the other function just does the conversion, and then we call that first function.

111
00:07:58,600 --> 00:08:05,770
And here in this line, force feeds inches, We pass that string as inputs, we get that dictionary

112
00:08:05,770 --> 00:08:06,640
as output.

113
00:08:06,640 --> 00:08:13,600
And then in here we use the feed value from that dictionary and the inches value from that dictionary

114
00:08:13,600 --> 00:08:17,680
to supply them to the convert function, which gets these two numbers.

115
00:08:17,680 --> 00:08:20,560
So those two numbers are converted into meters.

116
00:08:20,560 --> 00:08:23,800
Meters is returned here, therefore it is stored in result.

117
00:08:23,800 --> 00:08:30,040
And then we construct this f string and then in this if else conditional, we check if the result if

118
00:08:30,040 --> 00:08:34,510
meters is less than one and we print out different messages depending on that.

119
00:08:34,659 --> 00:08:39,400
So decouple your functions and decouple your output as well.

120
00:08:39,880 --> 00:08:47,170
And before I close this video, I cannot resist without making an analogy again with a cafe ordering

121
00:08:47,170 --> 00:08:51,100
example to illustrate your decoupling in another way.

122
00:08:51,100 --> 00:08:57,160
So when you go to the cafe and you order it to the barista, then the barista will give you the coffee.

123
00:08:57,160 --> 00:09:04,690
They will not put sugar inside the coffee or milk, but they may have a separate place there where you

124
00:09:04,690 --> 00:09:07,960
can go and put the sugar or the milk to your coffee.

125
00:09:07,960 --> 00:09:10,480
So they are decoupling the output.

126
00:09:10,480 --> 00:09:13,360
They don't want to give you the coffee plus the sugar inside.

127
00:09:13,360 --> 00:09:16,570
So then the coffee might not be usable.

128
00:09:16,570 --> 00:09:18,610
The same thing we did here.

129
00:09:18,610 --> 00:09:25,690
We return the number only, not the number with some extras on top of it, which can be unusable for

130
00:09:25,690 --> 00:09:28,210
other components in the program.

131
00:09:28,210 --> 00:09:30,220
So that's about decoupling output.

132
00:09:30,250 --> 00:09:37,990
Now, decoupling functions how it works in a goods cafe, you do the order to the barista, but then

133
00:09:37,990 --> 00:09:40,630
you pay the money to the cashier.

134
00:09:40,630 --> 00:09:45,220
So the cashier processes the payment as the barista processes the coffee.

135
00:09:45,220 --> 00:09:47,140
So here they have two functions.

136
00:09:47,140 --> 00:09:49,960
Each of them does something and does it good.

137
00:09:49,960 --> 00:09:53,680
Well, that at least happens in efficient cafes.

138
00:09:53,680 --> 00:10:00,430
So if you want to create quality programs, you also need you also want to decouple your functions.

139
00:10:00,460 --> 00:10:02,200
That's what I have for this lecture.

140
00:10:02,200 --> 00:10:04,930
I hope it makes sense and I'll talk to you later.

141
00:10:05,020 --> 00:10:05,620
Thanks.

