WEBVTT 1 00:00:01.950 --> 00:00:06.240 ok let's continue on what I might do is delete these extra lines first that I added 2 00:00:06.240 --> 00:00:11.719 ....and what we're going to do now as I mentioned at 3 00:00:11.719 --> 00:00:16.260 the end of the previous video was going to change the calls to center_text so that we 4 00:00:16.260 --> 00:00:21.130 can allow the output to go to a file so to do that it's actually quite simple we need 5 00:00:21.130 --> 00:00:37.220 to put on line 14...and then if I just in 6 00:00:37.220 --> 00:00:43.650 dent this code here the various ones we actually had and what we need to do is add 7 00:00:43.650 --> 00:00:47.460 an additional parameter and of course the paramter is gonna be the files so..... 8 00:00:47.460 --> 00:00:57.720 ...... 9 00:00:57.720 --> 00:01:08.260 ..... 10 00:01:08.260 --> 00:01:14.190 so now we've done the output should actually go to a file..... 11 00:01:14.190 --> 00:01:20.500 ....so now we've done that we should fine that when we write this it will write it to 12 00:01:20.500 --> 00:01:24.870 the current folders so we should see the file called centered appear here in the 13 00:01:24.870 --> 00:01:28.850 project window so just run that 14 00:01:28.850 --> 00:01:35.640 you can see that is centered file came up their and if we double-click that and have a look at it you can see there's our 15 00:01:35.640 --> 00:01:40.940 content correctly being sent to a file so that's obviously working nicely so 16 00:01:40.940 --> 00:01:43.920 that's one approach to creating a function that allows text to be printed 17 00:01:43.920 --> 00:01:48.289 centered on the screen but like everything else in programming there is always another way 18 00:01:48.289 --> 00:01:54.009 now if we've written this function back in Python 3.2 there was no flush parameter to print 19 00:01:54.009 --> 00:01:58.360 so we wouldn't have known to include a flush center_text now although the 20 00:01:58.360 --> 00:02:02.250 function would still work it would no longer provide the same functionalities 21 00:02:02.250 --> 00:02:07.750 print from Python 3.3 and onward so what we can do is we can take another approach instead 22 00:02:07.750 --> 00:02:13.170 and that's to write center_text so that it just returns a required string 23 00:02:13.170 --> 00:02:18.440 without making any attempt to actually print it and that's also good opportunity 24 00:02:18.440 --> 00:02:22.840 to explain about returning values from functions so what we're going to do here is 25 00:02:22.840 --> 00:02:27.040 changed the program first and then described what the code is doing so we are gonna close this 26 00:02:27.040 --> 00:02:32.090 down we don't need that anymore we got our file open again our functions.py file and we are going to change the 27 00:02:32.090 --> 00:02:36.700 actual method this line here what we need to do is instead of having a print here on 28 00:02:36.700 --> 00:02:42.940 line 12 what we want to do was do change that to make that a..... 29 00:02:42.940 --> 00:02:49.829 ....and we are going to get rid of this extra stuff at the end so we are only going to return the left margin plus 30 00:02:49.829 --> 00:02:59.739 the text so I'm going to delete the rest and now if we change it to where 31 00:02:59.739 --> 00:03:04.829 we had before here center_text we're going to change this I'm going to comment out 32 00:03:04.829 --> 00:03:08.040 the width which are not using anymore 33 00:03:08.040 --> 00:03:13.859 put this back to indent so there is no indentation we are gonna change the files or removed the file 34 00:03:13.859 --> 00:03:26.980 we are not gonna output it anymore...... 35 00:03:26.980 --> 00:03:37.470 ...... 36 00:03:37.470 --> 00:03:50.319 ....so as you can see the function hasn't really changed 37 00:03:50.319 --> 00:03:56.069 much at all instead of passing a string on to print we use a return key 38 00:03:56.069 --> 00:04:00.129 word to indicated that the string should be returned when the function is called 39 00:04:00.129 --> 00:04:04.750 looking at line 18 the center_text function is called with a 40 00:04:04.750 --> 00:04:10.970 string spam and eggs and it returned a string to be centered with a width of 80 characters so 41 00:04:10.970 --> 00:04:15.160 the print function then prints out that return value whatever value came back from the 42 00:04:15.160 --> 00:04:19.070 center_text function so it's normal to struggle this at first when 43 00:04:19.070 --> 00:04:22.610 passing your own functions to other functions but it's really no different 44 00:04:22.610 --> 00:04:27.500 things we've actually done such as and I'll give an example we've done this 45 00:04:27.500 --> 00:04:35.000 before in the course we put..... 46 00:04:35.000 --> 00:04:41.070 ..... 47 00:04:41.070 --> 00:04:48.200 ....so you can see we have done something similar there but again the 48 00:04:48.200 --> 00:04:54.010 concept is we got a function center_text and by typing this 49 00:04:54.010 --> 00:04:59.410 keyword it would return this statement what were doing is we are saying the output that was going to be printed 50 00:04:59.410 --> 00:05:03.860 we are returning that to wherever this was calles so in this case the center_text 51 00:05:03.860 --> 00:05:09.070 is how we called that particular method and then whatever is returned is 52 00:05:09.070 --> 00:05:13.050 in sort of soaked up by the print method automatically and then its printed out to the 53 00:05:13.050 --> 00:05:15.700 console I haven't actually run that's so we should just run it to make sure that 54 00:05:15.700 --> 00:05:21.180 still works you can see that it still does work and what I didn't do was 55 00:05:21.180 --> 00:05:24.370 actually delete these we should probably delete those as well 56 00:05:24.370 --> 00:05:30.660 so we don't need those anymore we got our list of arguments args and because this routine is now literally only 57 00:05:30.660 --> 00:05:35.610 returning the text it would not be appropriate to have those extra parameters and there shouldn't 58 00:05:35.610 --> 00:05:39.270 be a comma here there should be a plus so if I do that 59 00:05:39.270 --> 00:05:46.370 and now if we run it we should find the right out put as you can see so one thing 60 00:05:46.370 --> 00:05:51.370 that can help and can be less confusing if we assign the returned results to 61 00:05:51.370 --> 00:05:53.940 variables instead of printing them directly 62 00:05:53.940 --> 00:05:57.220 so we are gonna make a change to the code now just to show you an alternate way of 63 00:05:57.220 --> 00:06:04.100 doing this so instead of having the one line we could put...... 64 00:06:04.100 --> 00:06:17.580 ...... 65 00:06:17.580 --> 00:06:27.190 ..... 66 00:06:27.190 --> 00:06:38.520 ....so lets run it first to confirm if it works for 67 00:06:38.520 --> 00:06:39.300 us 68 00:06:39.300 --> 00:06:43.940 same result as you can see but what we've done is we've a assigned the value that has 69 00:06:43.940 --> 00:06:47.940 been returned from our function center_text and we have assigned that to 70 00:06:47.940 --> 00:06:52.300 variable and we printed it out so you can see the bottom line here is either by putting 71 00:06:52.300 --> 00:06:58.000 the name of the function in the call to print direct which I did previously or 72 00:06:58.000 --> 00:07:02.430 by actually assigning the value that was returned from a function into a variable 73 00:07:02.430 --> 00:07:08.010 and printing the variable out using print we got the same result so basically its a different 74 00:07:08.010 --> 00:07:11.970 approach to centering text and you could see that that's working nicely so this 75 00:07:11.970 --> 00:07:15.610 was a different approach to centering text because here we just created 76 00:07:15.610 --> 00:07:18.910 the string that should be printed but we left it to the print function 77 00:07:18.910 --> 00:07:24.330 to do the actual printing the only named parameters of print that we bothered about is sep 78 00:07:24.330 --> 00:07:28.380 anything else is not our concern so if someone else wants to send the centered 79 00:07:28.380 --> 00:07:32.160 text to a file they could do so in a way that they normally would so we could do 80 00:07:32.160 --> 00:07:40.270 something like this I'm just gonna get rid of these last 2 lines which we don't need 81 00:07:40.270 --> 00:07:48.090 and leave that in their so we got a reference in the source code and paste the code again so we could do something like..... 82 00:07:48.090 --> 00:08:04.260 ...so the actual calls to the 83 00:08:04.260 --> 00:08:12.440 function is going to be the same but when we go to print it will add this...... 84 00:08:12.440 --> 00:08:28.050 ..... 85 00:08:28.050 --> 00:08:34.800 ....so its still 86 00:08:34.800 --> 00:08:39.740 going to be outputted.....and for the next one we will do something similar..... 87 00:08:39.740 --> 00:08:55.220 ..... 88 00:08:55.220 --> 00:09:01.170 .....so in 89 00:09:01.170 --> 00:09:05.020 the case of the s1 s2 and s5 variables 90 00:09:05.020 --> 00:09:09.160 we are calling the method and saving the returned data that's come back from that 91 00:09:09.160 --> 00:09:15.330 method into a variable and outputting that variables on the next line but also I've got 2 92 00:09:15.330 --> 00:09:20.600 examples one on line 34 and one on line 35 where we are recording the method direct without 93 00:09:20.600 --> 00:09:26.630 assigning the return value to a variable temporarily before printing it out in other words 94 00:09:26.630 --> 00:09:31.040 we've use a mix of assigning the return value to a variable and printing it out really just 95 00:09:31.040 --> 00:09:35.660 to show that the output can be sent to a file in either way either formats is acceptable and if we just run 96 00:09:35.660 --> 00:09:43.220 that make sure it does work and you can see the file up here their so lets close down the run window and open menu and 97 00:09:43.220 --> 00:09:47.310 you can see we still got the data their so that's it that's the basics of creating your own 98 00:09:47.310 --> 00:09:50.810 functions I'm gonna stop the video here in the next video we will practice 99 00:09:50.810 --> 00:09:55.020 creating a few more functions and then we'll actually have a challenge to 100 00:09:55.020 --> 00:09:59.180 hopefully help you understand the concepts even better so see you in the next 101 00:09:59.180 --> 00:09:59.400 video