Hi, and welcome to today's bonus example.
In yesterday's bonus example, we worked on this code, so if you haven't seen that video yet, please
do so because that video is related to this one.
In a nutshell, what we did is we asked the user to enter Ft and inches, so we got that as a string
here.
And then this convert function extracted the numbers from that string.
So if the user entered 312, for example, we extracted three and 12 and we convert them into floats
here and there and we calculated meters out of those two numbers.
So three and 12, we multiply that by that number and that's by that and we got meters.
So this was good to return a number instead of a string where you had meters inside the string and you
had some other information here text.
So that was not good because we couldn't use that output in other parts of the codes as we do here.
So here we want to.
Compare that meters with some other value.
So it's good to decouple your output.
The problem this brought about was that now what if we want to print out the feed value that the user
entered so feeds?
We cannot access that value from that function because that feed is a local variable.
So we cannot say that and same four inches is equal to we can get meters because meters is the output.
So when we return a variable, a local variable, that variable can be accessed through the function
call.
So that's okay.
We can.
Access results.
But how do we access FT and inches?
Well, the problem lies again in decoupling.
Today, we're going to talk about decoupling functions.
Yesterday, we talked about decoupling output.
Now, this function is doing too much.
It's doing two things.
First, it is passing that string.
So the user enters FT and inches and the function is splitting it.
And then it's extracting numbers from that string and then it converts those to numbers into meters.
However, the name of the function is convert.
So this function is doing too much functions should do one thing and do it well.
So the solution here is decoupling.
And the way we do decoupling here is by creating two different functions.
Let's call this pass or extract whatever we like.
So pause and feed inches goes as the inputs of this pause function, and then this code goes in here.
So we get ports, we say feed inches, split feed is converted into floats, interest is converted into
float as well.
And then we return those two numbers.
So we make these two numbers basically available to be accessed from the other components of the script.
You can see fit and inches.
So if I demonstrate you what I'm doing here, this should work.
Like this lets me paste this function here in the python console.
So pause if we call it and we say 412 and we press enter, what we get in return is a tuple with these
two values.
So when you return to variables, they're what you get in return is not one value, but two values enclosed
in a tuple.
So if you check the type of that value, it's going to be a tuple you see or what you could do.
If you want to improve that even further, you could use a dictionary.
You could say feed is ft and inches as inches.
You remember that with dictionaries we have pairs of keys and values.
So the key here is the string.
Its value is the number that the variable feeds contains.
Then we have another pair of key and value.
So if I get this functional, I define it in here in the console, execute it and then call it pause
for 12.
What we get is a dictionary.
So now we have feeds and that is the value of feed and that's the value of inches.
So that's a dictionary, which means you can access that value by providing here the name of its key
feed as a string, and it gives you 4.0 and you can do the same four inches.
12.0.
So going back to the program in the F string here, we could say to get feet, we say pause.
You call pause with feet and inches inside and then access feeds from that.
Or even better, you could get that expression and do that outside.
And you see past.
Is that so?
That should give you the dictionary as you saw here.
So that gives the dictionary.
Right.
And then in here you see post and you extract feed.
You need to use single quotes here since you have used double quotes outside the string.
So for that string use single quotes.
So again, that returns a dictionary, that dictionary and that extracts that feed value from the dictionary.
Same, we do four interest post entries and that is equal to result.
So result comes from convert.
Now convert we need to work on this function as well.
Convert now shouldn't be getting a string.
It should get the units to convert, which is ft and inches.
So it should get these as numbers and it shouldn't be doing any conversion from string to float.
Just get numbers, return numbers.
So here in convert then we say post feed to pass the feed value.
The problem is this is highlighted in red.
It says that this variable has not been defined, so this variable should be defined before it's used
here.
We are using it here, but we haven't defined it in this part.
Therefore I just cut this from there and paste it up here.
So we first get the feeds and inches and then we pass those feed and inches in here, converts again
inches make a break line there, and that's the codes.
Finally, let's run it.
Feed on inches 311 and let's see, we've got an error bonus 13, line 17 which is in here.
It says that function is not subscript double.
Yeah.
So here I did convert.
I should have done post.
Let's run again.
310 Presenter Three feet and ten inches is equal to 116 kids can use the slide because the kid is higher
than one metre.
So that's how it should be working.
That's a complete code.
So again, to wrap it up, we get a string here and we want a function which does parsing of that string.
We want to extract the numbers from that string.
So it's better to have that parsing inside one single function.
So functions that way are decoupled.
That solves the problem.
As I explained to you, that solves the problem of being able to access those things from that function.
So we access those things and we use them in this f string here.
And then the other function just does the conversion, and then we call that first function.
And here in this line, force feeds inches, We pass that string as inputs, we get that dictionary
as output.
And then in here we use the feed value from that dictionary and the inches value from that dictionary
to supply them to the convert function, which gets these two numbers.
So those two numbers are converted into meters.
Meters is returned here, therefore it is stored in result.
And then we construct this f string and then in this if else conditional, we check if the result if
meters is less than one and we print out different messages depending on that.
So decouple your functions and decouple your output as well.
And before I close this video, I cannot resist without making an analogy again with a cafe ordering
example to illustrate your decoupling in another way.
So when you go to the cafe and you order it to the barista, then the barista will give you the coffee.
They will not put sugar inside the coffee or milk, but they may have a separate place there where you
can go and put the sugar or the milk to your coffee.
So they are decoupling the output.
They don't want to give you the coffee plus the sugar inside.
So then the coffee might not be usable.
The same thing we did here.
We return the number only, not the number with some extras on top of it, which can be unusable for
other components in the program.
So that's about decoupling output.
Now, decoupling functions how it works in a goods cafe, you do the order to the barista, but then
you pay the money to the cashier.
So the cashier processes the payment as the barista processes the coffee.
So here they have two functions.
Each of them does something and does it good.
Well, that at least happens in efficient cafes.
So if you want to create quality programs, you also need you also want to decouple your functions.
That's what I have for this lecture.
I hope it makes sense and I'll talk to you later.
Thanks.