1
00:00:04,980 --> 00:00:09,600
As we discussed in the last video, not
all functions return a useful value.

2
00:00:09,600 --> 00:00:12,980
Sometimes, you'll want to write a
function that does something but doesn't

3
00:00:12,990 --> 00:00:17,040
produce a result. I'd like to thank
Aubrey Love, a student in this course, for

4
00:00:17,040 --> 00:00:21,419
asking the question that led to this
next function. Aubrey wanted to print

5
00:00:21,419 --> 00:00:24,750
some instructions for an adventure game,
and wanted them to be neatly formatted

6
00:00:24,750 --> 00:00:29,189
as a banner, when the program started.
I'll create a new Python file called

7
00:00:29,189 --> 00:00:37,400
banner.py for this example.

8
00:00:37,450 --> 00:00:41,320
So we'll start by seeing the sort of
output we want to produce. What I'm going

9
00:00:41,320 --> 00:00:45,340
to do is paste in some code and run the
program, so that we can see what we want

10
00:00:45,340 --> 00:00:48,070
to produce. So don't type this code in. We're going

11
00:00:48,070 --> 00:00:52,780
to write a function to take care of the
formatting for us. Okay we'll run the

12
00:00:52,780 --> 00:00:57,330
output, or run the program rather, to get
the output,

13
00:00:58,620 --> 00:01:03,040
and there's the output on-screen.
So formatting your code in the editor

14
00:01:03,040 --> 00:01:06,900
would be a bit tedious. You'd have to
count the spaces that appear either side

15
00:01:06,900 --> 00:01:12,740
of the text, for each line. Each line is
actually centered between the asterisks,

16
00:01:12,740 --> 00:01:17,600
and it's a bit boring counting spaces. We've got computers to do the boring stuff for us,

17
00:01:17,600 --> 00:01:21,700
so let's write a function to take
care of formatting the text. What I'm

18
00:01:21,700 --> 00:01:27,300
going to do is delete all that code, and
instead paste in a function.

19
00:01:27,659 --> 00:01:31,350
Alright, so there's the code. Alright, so
pause the video while you type in the

20
00:01:31,350 --> 00:01:35,399
function. There's nothing complicated in
this function, but I'll quickly run

21
00:01:35,399 --> 00:01:38,820
through what it's doing.
We start by binding a variable to the

22
00:01:38,820 --> 00:01:42,509
width that we want our banner to be.
That's currently 80, as you can see on

23
00:01:42,509 --> 00:01:47,320
line two. Using a variable makes it much
easier to change the width if we want

24
00:01:47,320 --> 00:01:51,420
Next we check the length of the text. If
it's too long, our banner won't look

25
00:01:51,420 --> 00:01:54,899
right. At this point in the course, we
haven't covered everything we need to

26
00:01:54,900 --> 00:01:58,980
handle this situation,
so rather than introducing something new,

27
00:01:58,980 --> 00:02:03,000
I've just printed a long and formatted
message. That'll make it obvious that

28
00:02:03,000 --> 00:02:08,068
something's going wrong. What we really
should do, is make the program crash. I'll

29
00:02:08,068 --> 00:02:11,940
do that when we test that bit of code, so
you can see the correct way to handle

30
00:02:11,940 --> 00:02:15,800
invalid situations, but for now that
message will do the job.

31
00:02:15,800 --> 00:02:20,340
The main body of the function prints
a row of asterisks, if the calling code

32
00:02:20,340 --> 00:02:25,200
passed an asterisk to our function.
That's the code on lines seven and eight.

33
00:02:25,200 --> 00:02:30,810
Otherwise, it prints the text with two
asterisks at the start and end. We use

34
00:02:30,810 --> 00:02:35,489
the string center method to take care of
centering the text. You can check the

35
00:02:35,489 --> 00:02:38,489
method in the string methods
documentation. We've looked at that a few

36
00:02:38,489 --> 00:02:42,900
times now. 4 is subtracted from the
screen width because we're printing

37
00:02:42,900 --> 00:02:48,080
4 asterisks - 2 each side of the text. We
use normal string formatting with a

38
00:02:48,090 --> 00:02:53,370
placeholder, to print the centered string
inside the asterisks. If that looks

39
00:02:53,370 --> 00:02:57,750
confusing, remember that you can split
code up, and that can make it easier to

40
00:02:57,750 --> 00:03:01,820
understand when you're learning. For
example, I could cut out text.center -

41
00:03:01,820 --> 00:03:07,440
this here, this entire call - including the
parentheses screen_width take 4.

42
00:03:07,440 --> 00:03:11,900
I could cut that. We come up to the
previous line and assign that expression

43
00:03:11,910 --> 00:03:16,400
to another variable. Let's do that, so
I'm going to call this one centered

44
00:03:16,400 --> 00:03:21,580
text is equal to, and I'll paste it in again,
and then we'll use that variable,

45
00:03:21,580 --> 00:03:26,520
centered_text. That can help with
the confusion. So if you find that easier

46
00:03:26,520 --> 00:03:30,420
to understand, then it's fine to write
your code that way. In time, you'll avoid

47
00:03:30,420 --> 00:03:34,050
the intermediate steps and start
combining statements, such as those on

48
00:03:34,050 --> 00:03:39,000
lines 10 and 12. Alright, so let's write
some code now, that calls our function,

49
00:03:39,000 --> 00:03:44,010
and come down here and start on lines 15, or line 15.
Remember that we leave two

50
00:03:44,010 --> 00:03:47,940
blank lines after a function definition.
Now you could use any text you want to

51
00:03:47,940 --> 00:03:51,900
create your banner. I'm going to paste in
part of the Monty Python song, Always look on

52
00:03:51,900 --> 00:03:56,340
the bright side of life.
Okay, so there's what I'll paste it in,

53
00:03:56,340 --> 00:04:00,320
noting that we're calling the banner
text function, which is what we called

54
00:04:00,330 --> 00:04:05,190
our function on line one. So we're
calling that instead of print. The banner

55
00:04:05,190 --> 00:04:09,120
text function takes care of printing, but
also formats the text for us.

56
00:04:09,120 --> 00:04:12,200
So if we run the program now,

57
00:04:12,490 --> 00:04:16,510
you can see we've got our neatly
formatted banner. So that's a lot easier

58
00:04:16,510 --> 00:04:21,380
than counting spaces. We've got a simple
way to print neatly formatted output,

59
00:04:21,380 --> 00:04:26,760
just by passing a string to the banner
underscore text function. So we pass an

60
00:04:26,770 --> 00:04:33,820
asterisk to the function on lines 15 and
25. When the function gets an asterisk,

61
00:04:33,820 --> 00:04:39,820
it prints a row of asterisks. Line 20 isn't
very intuitive. We want to include a

62
00:04:39,820 --> 00:04:44,590
blank line, but we don't want a full row of
asterisks. Passing a space as the

63
00:04:44,590 --> 00:04:49,480
argument produces the effect we want. The
space gets centered, with asterisks at

64
00:04:49,480 --> 00:04:54,120
either end. And again, we'll run that to
see the output.

65
00:04:54,120 --> 00:04:58,830
So that's not very obvious though - that
functionality - to anyone who uses the

66
00:04:58,830 --> 00:05:03,210
function. If you want to print a blank
line with a built-in print function, you

67
00:05:03,210 --> 00:05:08,160
call print without providing an argument.
Ideally, our banner text function should

68
00:05:08,160 --> 00:05:12,600
work in the same way. We'll get to do
that soon. Before that though, I want to

69
00:05:12,600 --> 00:05:16,590
finish this video with a comment about
our function's return value.

70
00:05:16,590 --> 00:05:21,330
So basically, we're not using it. What I
mean is, we wouldn't do something like

71
00:05:21,330 --> 00:05:28,710
this; result equals banner_text,

72
00:05:28,710 --> 00:05:32,190
Nothing is returned.

73
00:05:32,319 --> 00:05:35,979
But if I hover over the warning there,
Function banner_text doesn't

74
00:05:35,979 --> 00:05:40,479
return anything. But that's not strictly true,
in fact, as I've alluded to previously.

75
00:05:40,479 --> 00:05:45,309
Every Python function returns something.
Functions return None if you don't

76
00:05:45,309 --> 00:05:50,320
specify a value to return. We can check
that by printing out the result again again.

77
00:05:50,320 --> 00:05:54,200
And again, I know you've seen this previously, so result.

78
00:05:56,020 --> 00:05:58,790
We can see None there, down the bottom,

79
00:05:58,790 --> 00:06:05,340
as the last line of output. So
our banner_text function is designed,

80
00:06:05,340 --> 00:06:10,580
or intended to perform an action, not to
return a result. It returns None, because

81
00:06:10,580 --> 00:06:16,310
that's what Python does, but the return
value is no use to us here. Now you know

82
00:06:16,310 --> 00:06:20,060
why you get None printed out in situations like this.

83
00:06:20,060 --> 00:06:26,160
And I'll type in an example here, so
numbers is equal to square brackets;

84
00:06:26,160 --> 00:06:40,380
four two seven five eight three nine
six one, print numbers dot sort.

85
00:06:41,540 --> 00:06:44,290
If we run this,

86
00:06:44,380 --> 00:06:48,340
hopefully, that now makes it clear why
you get None printed out when you do

87
00:06:48,340 --> 00:06:51,820
something like I've just done there,
on lines 30 and 31, or line 31

88
00:06:51,820 --> 00:06:56,920
specifically. The dot sort method sorts
the list in place. So it's performing an

89
00:06:56,920 --> 00:07:01,360
action, sorting the list here, and doesn't
return a useful value, and that's why

90
00:07:01,360 --> 00:07:05,110
we've got None in the output. Alright,
so I'm gonna finish this video now with

91
00:07:05,110 --> 00:07:09,210
a quick summary.

92
00:07:52,020 --> 00:07:53,580
Let's move on now to the next video,

93
00:07:53,580 --> 00:07:56,540
where we'll start talking about
handling invalid arguments.

94
00:07:56,540 --> 00:07:59,620
See you in the next video.

