1
00:00:05,440 --> 00:00:09,730
ok let's continue on what I might do is
delete these extra lines first that I added

2
00:00:09,730 --> 00:00:15,200
....and what
we're going to do now as I mentioned at

3
00:00:15,200 --> 00:00:19,750
the end of the previous video was going
to change the calls to center_text so that we

4
00:00:19,750 --> 00:00:24,620
can allow the output to go to a file so to do
that it's actually quite simple we need

5
00:00:24,620 --> 00:00:40,710
to put on line 14...and then if I just in

6
00:00:40,710 --> 00:00:47,140
dent this code here the various ones we actually
had and what we need to do is add

7
00:00:47,140 --> 00:00:50,950
an additional parameter and of course the paramter is gonna be the files so.....

8
00:00:50,950 --> 00:01:01,210
......

9
00:01:01,210 --> 00:01:11,750
.....

10
00:01:11,750 --> 00:01:17,680
so now we've done the output should
actually go to a file.....

11
00:01:17,680 --> 00:01:23,990
....so now we've done that we should fine that when we write this it will write it to

12
00:01:23,990 --> 00:01:28,360
the current folders so we should see the file called centered appear here in the

13
00:01:28,360 --> 00:01:32,340
project window so just run that

14
00:01:32,340 --> 00:01:39,130
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:39,130 --> 00:01:44,430
content correctly being sent to a file so that's obviously working nicely so

16
00:01:44,430 --> 00:01:47,410
that's one approach to creating a
function that allows text to be printed

17
00:01:47,410 --> 00:01:51,770
centered on the screen but like everything
else in programming there is always another way

18
00:01:51,770 --> 00:01:57,490
now if we've written this function back in Python 3.2 there was no flush parameter to print

19
00:01:57,490 --> 00:02:01,850
so we wouldn't have known to include a flush center_text now although the

20
00:02:01,850 --> 00:02:05,740
function would still work it would no
longer provide the same functionalities

21
00:02:05,740 --> 00:02:11,240
print from Python 3.3 and onward so what we can do
is we can take another approach instead

22
00:02:11,240 --> 00:02:16,660
and that's to write center_text so that it just returns a required string

23
00:02:16,660 --> 00:02:21,930
without making any attempt to actually
print it and that's also good opportunity

24
00:02:21,930 --> 00:02:26,330
to explain about returning values from
functions so what we're going to do here is

25
00:02:26,330 --> 00:02:30,530
changed the program first and then
described what the code is doing so we are gonna close this

26
00:02:30,530 --> 00:02:35,580
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:35,580 --> 00:02:40,190
actual method this line here what we
need to do is instead of having a print here on

28
00:02:40,190 --> 00:02:46,430
line 12 what we want to do was do change
that to make that a.....

29
00:02:46,430 --> 00:02:53,310
....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:53,310 --> 00:03:03,220
the text so I'm going to delete the rest and now if we change it to where

31
00:03:03,220 --> 00:03:08,310
we had before here center_text we're
going to change this I'm going to comment out

32
00:03:08,310 --> 00:03:11,530
the width which are not using anymore

33
00:03:11,530 --> 00:03:17,340
put this back to indent so there is no indentation
we are gonna change the files or removed the file

34
00:03:17,340 --> 00:03:30,470
we are not gonna output it anymore......

35
00:03:30,470 --> 00:03:40,960
......

36
00:03:40,960 --> 00:03:53,800
....so as you can
see the function hasn't really changed

37
00:03:53,800 --> 00:03:59,550
much at all instead of passing a string
on to print we use a return key

38
00:03:59,550 --> 00:04:03,610
word to indicated that the string should be
returned when the function is called

39
00:04:03,610 --> 00:04:08,240
looking at line 18 the center_text function is called with a

40
00:04:08,240 --> 00:04:14,460
string spam and eggs and it returned a string to be centered with a width of 80 characters so

41
00:04:14,460 --> 00:04:18,649
the print function then prints out that
return value whatever value came back from the

42
00:04:18,649 --> 00:04:22,560
center_text function so it's
normal to struggle this at first when

43
00:04:22,560 --> 00:04:26,100
passing your own functions to other
functions but it's really no different

44
00:04:26,100 --> 00:04:30,990
things we've actually done such as
and I'll give an example we've done this

45
00:04:30,990 --> 00:04:38,490
before in the course we put.....

46
00:04:38,490 --> 00:04:44,560
.....

47
00:04:44,560 --> 00:04:51,690
....so you can see we have done
something similar there but again the

48
00:04:51,690 --> 00:04:57,500
concept is we got a function center_text and by typing this

49
00:04:57,500 --> 00:05:02,900
keyword it would return this statement what were doing is we are saying the
output that was going to be printed

50
00:05:02,900 --> 00:05:07,350
we are returning that to wherever this
was calles so in this case the center_text

51
00:05:07,350 --> 00:05:12,560
is how we called that particular
method and then whatever is returned is

52
00:05:12,560 --> 00:05:16,540
in sort of soaked up by the print method
automatically and then its printed out to the

53
00:05:16,540 --> 00:05:19,190
console I haven't actually run that's so we
should just run it to make sure that

54
00:05:19,190 --> 00:05:24,670
still works you can see that it still
does work and what I didn't do was

55
00:05:24,670 --> 00:05:27,860
actually delete these we should probably
delete those as well

56
00:05:27,860 --> 00:05:34,150
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:34,150 --> 00:05:39,100
returning the text it would not be appropriate
to have those extra parameters and there shouldn't

58
00:05:39,100 --> 00:05:42,760
be a comma here there should be a plus so if I do that

59
00:05:42,760 --> 00:05:49,860
and now if we run it we should find the right out put as you can see so one thing

60
00:05:49,860 --> 00:05:54,860
that can help and can be less confusing
if we assign the returned results to

61
00:05:54,860 --> 00:05:57,430
variables instead of printing them
directly

62
00:05:57,430 --> 00:06:00,710
so we are gonna make a change to the code
now just to show you an alternate way of

63
00:06:00,710 --> 00:06:07,590
doing this so instead of having the one line we could put......

64
00:06:07,590 --> 00:06:21,070
......

65
00:06:21,070 --> 00:06:30,680
.....

66
00:06:30,680 --> 00:06:42,010
....so lets run it first to confirm if it works for

67
00:06:42,010 --> 00:06:42,790
us

68
00:06:42,790 --> 00:06:47,430
same result as you can see but what we've
done is we've a assigned the value that has

69
00:06:47,430 --> 00:06:51,430
been returned from our function
center_text and we have assigned that to

70
00:06:51,430 --> 00:06:55,790
variable and we printed it out so you can
see the bottom line here is either by putting

71
00:06:55,790 --> 00:07:01,490
the name of the function in the call to
print direct which I did previously or

72
00:07:01,490 --> 00:07:05,920
by actually assigning the value that was
returned from a function into a variable

73
00:07:05,920 --> 00:07:11,500
and printing the variable out using print we got the
same result so basically its a different

74
00:07:11,500 --> 00:07:15,460
approach to centering text and you could see that that's working nicely so this

75
00:07:15,460 --> 00:07:19,100
was a different approach to
centering text because here we just created

76
00:07:19,100 --> 00:07:22,400
the string that should be printed but
we left it to the print function

77
00:07:22,400 --> 00:07:27,820
to do the actual printing the only named
parameters of print that we bothered about is sep

78
00:07:27,820 --> 00:07:31,870
anything else is not our concern so if
someone else wants to send the centered

79
00:07:31,870 --> 00:07:35,650
text to a file they could do so in a way that
they normally would so we could do

80
00:07:35,650 --> 00:07:43,760
something like this I'm just gonna get rid of these last 2 lines which we don't need

81
00:07:43,760 --> 00:07:51,580
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:51,580 --> 00:08:07,750
...so the actual calls to the

83
00:08:07,750 --> 00:08:15,930
function is going to be the same but when we go to
print it will add this......

84
00:08:15,930 --> 00:08:31,540
.....

85
00:08:31,540 --> 00:08:38,289
....so its still

86
00:08:38,289 --> 00:08:43,230
going to be outputted.....and for the next one we will do something similar.....

87
00:08:43,230 --> 00:08:58,710
.....

88
00:08:58,710 --> 00:09:04,660
.....so in

89
00:09:04,660 --> 00:09:08,510
the case of the s1 s2 and s5
variables

90
00:09:08,510 --> 00:09:12,650
we are calling the method and saving the
returned data that's come back from that

91
00:09:12,650 --> 00:09:18,820
method into a variable and outputting that
variables on the next line but also I've got 2

92
00:09:18,820 --> 00:09:24,090
examples one on line 34 and one on line 35 where we are recording the method direct without

93
00:09:24,090 --> 00:09:30,120
assigning the return value to a variable
temporarily before printing it out in other words

94
00:09:30,120 --> 00:09:34,530
we've use a mix of assigning the
return value to a variable and printing it out really just

95
00:09:34,530 --> 00:09:39,150
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:39,150 --> 00:09:46,710
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:46,710 --> 00:09:50,800
you can see we still got the data their so that's it
that's the basics of creating your own

98
00:09:50,800 --> 00:09:54,300
functions I'm gonna stop the video here
in the next video we will practice

99
00:09:54,300 --> 00:09:58,510
creating a few more functions and then
we'll actually have a challenge to

100
00:09:58,510 --> 00:10:02,670
hopefully help you understand the
concepts even better so see you in the next

101
00:10:02,670 --> 00:10:02,890
video

