1
00:00:05,359 --> 00:00:08,080
Alright, so writing a Fibonacci function.

2
00:00:08,080 --> 00:00:11,200
Now that we know what we're doing, we can
write the code to create Fibonacci

3
00:00:11,200 --> 00:00:13,599
numbers.
What I'm going to do is add the function

4
00:00:13,599 --> 00:00:18,240
to our functions.py module, after
the palindrome_sentence function,

5
00:00:18,240 --> 00:00:21,520
down here.
We're going to leave two blank lines

6
00:00:21,520 --> 00:00:25,519
between functions, as always, so I'm going
to start the code on line 47.

7
00:00:25,519 --> 00:00:28,640
And remember that I deleted the old
comments, and the diagnostic print

8
00:00:28,640 --> 00:00:31,599
statement, in the solution
to the last challenge.

9
00:00:31,599 --> 00:00:36,000
Let's start by typing
def fibonacci

10
00:00:36,000 --> 00:00:41,680
parentheses n colon.
I'm going to add a docstring, so three

11
00:00:41,680 --> 00:00:45,680
double quotes.
I've called the function fibonacci and

12
00:00:45,680 --> 00:00:50,480
it's got a single parameter,
n. Next, I'll write the docstring.

13
00:00:50,480 --> 00:00:53,120
Alright, so I'm going to start now with the
one line description, and for this

14
00:00:53,120 --> 00:00:56,079
function, that's all I'm going to include.
So I'm not going to press enter here,

15
00:00:56,080 --> 00:01:01,520
I'm just going to type Return the nth
fibonacci number for positive n,

16
00:01:01,540 --> 00:01:06,400
and n, I'm going to include in backticks,

17
00:01:10,880 --> 00:01:17,680
and using backtick again for n,
like so. So there's really no need to add

18
00:01:17,680 --> 00:01:20,880
anything more than that.
Documenting the parameter isn't going to

19
00:01:20,880 --> 00:01:24,240
add anything that isn't obvious already.
If someone doesn't know what a Fibonacci

20
00:01:24,240 --> 00:01:26,000
number is, they won't
be using our function

21
00:01:26,000 --> 00:01:29,620
to create one. There's also no point
in documenting the return value.

22
00:01:29,620 --> 00:01:33,600
Our one line description says
what the function returns. Now check

23
00:01:33,600 --> 00:01:36,400
out this section,
Economy of Expression, which I'll bring

24
00:01:36,400 --> 00:01:41,960
on screen now - specifically 725 here, 
Economy of Expression.

25
00:01:41,960 --> 00:01:44,079
The suggestion there is to err on the

26
00:01:44,079 --> 00:01:47,520
side of being succinct.
Now of course, you might disagree here.

27
00:01:47,520 --> 00:01:49,759
If you think this function
needs more documentation,

28
00:01:49,759 --> 00:01:52,960
then go ahead and certainly include it.
When writing your code,

29
00:01:52,960 --> 00:01:56,799
you decide how it should be documented.
Alright, so now for the code. Let's go

30
00:01:56,799 --> 00:02:00,079
back to IntelliJ.
Alright, so on the next line, after our

31
00:02:00,080 --> 00:02:04,600
Docstring, I'm going to start by working 
out how to handle the first two values.

32
00:02:04,600 --> 00:02:08,419
If you recall, the first two values in the
Fibonacci sequence are zero and one.

33
00:02:08,419 --> 00:02:13,480
So let's try typing if 0 is
less than equal to n,

34
00:02:13,480 --> 00:02:18,400
less than equal to 1 colon, return n.

35
00:02:18,480 --> 00:02:22,959
So what we're doing there, is saying if n
is 0, we'll return 0. If n is 1,

36
00:02:22,960 --> 00:02:28,860
we'll return 1. Now if we don't return, we 
need to add up two values to get the third.

37
00:02:28,860 --> 00:02:31,260
We know that the first two
values are zero and one,

38
00:02:31,260 --> 00:02:36,879
so I'm going to store them in variables.
On line 52, I'm going to type n

39
00:02:36,880 --> 00:02:43,360
_minus1 and n_minus 2

40
00:02:43,480 --> 00:02:48,239
equals 1 comma 0. Alright,
now that I've done that,

41
00:02:48,239 --> 00:02:51,599
the rest of the code is just a loop that
adds up the last two values,

42
00:02:51,599 --> 00:02:56,080
and produces the next result. So let's
type the code in; it's going to be for

43
00:02:56,080 --> 00:03:02,720
f in range, parentheses
n minus one colon.

44
00:03:02,720 --> 00:03:08,080
We're going to type result is
equal to n_minus2

45
00:03:08,080 --> 00:03:16,319
plus n_minus1. On the next
line, n_minus2 is equal to

46
00:03:16,319 --> 00:03:19,519
n_minus1,

47
00:03:19,760 --> 00:03:27,440
and n_minus1 is equal to
result. So loop goes around n minus one

48
00:03:27,440 --> 00:03:30,640
times, to produce the Fibonacci number for n.

49
00:03:30,640 --> 00:03:35,260
If n is two, the loop goes around once
and produces the result one -

50
00:03:35,260 --> 00:03:39,120
it adds zero and one. If n is three,
the loop goes around twice.

51
00:03:39,120 --> 00:03:42,560
So the first time result would be 1. It
then goes around again,

52
00:03:42,560 --> 00:03:47,200
and adds 1 to 1 giving the result
2, and so on. Use the debugger and step

53
00:03:47,200 --> 00:03:49,200
through the code if you're
not sure how it works,

54
00:03:49,200 --> 00:03:53,360
but it's just adding the last two
numbers to produce the next one.

55
00:03:53,360 --> 00:03:58,080
Okay, so when the loop terminates, we want
to return the result, so let's do that as well.

56
00:03:58,080 --> 00:04:00,640
Down here, we're going to go back to the

57
00:04:00,640 --> 00:04:06,640
same level as before and return
result. Now I've got a warning, if we hover

58
00:04:06,640 --> 00:04:10,159
over result here, that says
Local variable result

59
00:04:10,159 --> 00:04:14,400
might be referenced before assignment.
Always pay attention to any warnings

60
00:04:14,400 --> 00:04:16,720
that you get.
There are a few occasions when you can

61
00:04:16,720 --> 00:04:19,680
ignore them, but most of the time you
should fix them.

62
00:04:19,680 --> 00:04:23,360
In this case, the variable result won't
be bound to a value,

63
00:04:23,360 --> 00:04:27,840
if the calling code passes a negative
number. Our Docstring does say that n

64
00:04:27,840 --> 00:04:30,800
should be positive -
you see that at the top of the screen -

65
00:04:30,800 --> 00:04:34,000
but we should still handle negative
values in some way.

66
00:04:34,000 --> 00:04:37,759
You've already seen how to raise a value
exception, and here's one way to deal

67
00:04:37,760 --> 00:04:40,580
with this. I'm going to return none.

68
00:04:40,580 --> 00:04:45,280
Although there's a negafibonacci sequence,
the Fibonacci sequence is only defined

69
00:04:45,280 --> 00:04:47,840
for positive values.
So what I'm going to do is come back up

70
00:04:47,840 --> 00:04:53,199
here to line 54,
and initialize result to None. So result

71
00:04:53,199 --> 00:04:59,040
equals None, and I'm going to add a
new line at the end there, as well, as I

72
00:04:59,040 --> 00:05:02,960
always do. So that's fixed the error - there's
no longer a warning on line 60

73
00:05:02,960 --> 00:05:08,479
and we're ready to test our function.
So here what we can do now, is on line 63,

74
00:05:08,479 --> 00:05:11,840
I'm going to add a for loop to test our
fibonacci function, and remember to

75
00:05:11,840 --> 00:05:14,960
leave two blank lines as
I've done, after the function definition,

76
00:05:14,960 --> 00:05:16,880
or after the end of the function, I should say.

77
00:05:16,880 --> 00:05:21,120
Alright, so I'm going to test the first
36 fibonacci numbers with a loop.

78
00:05:21,120 --> 00:05:27,840
So I'm going to type for i in range
36 in parentheses colon, and print

79
00:05:27,840 --> 00:05:35,280
parentheses i comma fibonacci
i in parentheses.

80
00:05:35,280 --> 00:05:39,080
We run the results, or run the program
rather, to see the results.

81
00:05:39,080 --> 00:05:45,360
I'm going to move the cog over here,
Move To Right Top,

82
00:05:45,400 --> 00:05:50,080
which makes it a bit easier to see the
results, so I'll move this over a little bit.

83
00:05:53,020 --> 00:05:55,120
Basically now, you can see the output and

84
00:05:55,120 --> 00:05:58,400
our function seems to be working.
Now of course, you should validate the

85
00:05:58,400 --> 00:06:02,639
results. You could use google to find
higher Fibonacci numbers than were shown,

86
00:06:02,639 --> 00:06:06,000
in that web page we looked at earlier, or
you could use a spreadsheet.

87
00:06:06,000 --> 00:06:09,600
Also, make sure you test the function
with negative numbers. It should return

88
00:06:09,600 --> 00:06:12,720
None, in that case. I won't do that in
this video,

89
00:06:12,720 --> 00:06:16,480
but I will test our Docstring, which
should tip you off that there's going to

90
00:06:16,480 --> 00:06:19,980
be something wrong with it.
I'll hover over here to fibonacci on line 64,

91
00:06:19,980 --> 00:06:23,760
or I could do a Ctrl-Q to bring up the documentation -

92
00:06:23,760 --> 00:06:27,440
the quick documentation.
So the documentation appeared, but you

93
00:06:27,440 --> 00:06:29,759
can see that the formatting has gone a
bit funny.

94
00:06:29,759 --> 00:06:33,039
We've got a single backtick after the
first n,

95
00:06:33,039 --> 00:06:37,680
and a single one before the last n. What
we expected was for each of the

96
00:06:37,680 --> 00:06:39,900
references to n
for it to be in a different font.

97
00:06:39,900 --> 00:06:42,560
At the very least, we'd want them to have the
backtick before

98
00:06:42,560 --> 00:06:46,479
and after. So you may come across this
and spend many frustrating hours trying

99
00:06:46,479 --> 00:06:49,280
to fix it.
They'd be frustrating hours because you

100
00:06:49,280 --> 00:06:53,520
can't fix it.
The problem happens because reStructuredText

101
00:06:53,520 --> 00:06:56,680
doesn't allow partial highlighting of a word.

102
00:06:56,680 --> 00:06:58,080
Formatting characters such as the

103
00:06:58,080 --> 00:07:01,680
backtick can't be followed by an
alphanumeric character.

104
00:07:01,680 --> 00:07:05,199
The reStructuredText documentation does
mention that this restriction may be

105
00:07:05,200 --> 00:07:08,660
lifted in the future,
but it still applies at the moment.

106
00:07:08,660 --> 00:07:12,960
So we included this example deliberately,
so that you can see the problem and be

107
00:07:12,960 --> 00:07:17,120
spared the frustration.
So the easy way to fix this

108
00:07:17,120 --> 00:07:21,120
is just to put a space before the th.

109
00:07:23,919 --> 00:07:28,479
I'm going to line 64 and we'll call up the
documentation again.

110
00:07:28,479 --> 00:07:31,759
You can see that it's looking much
better. Both occurrences of n are now

111
00:07:31,759 --> 00:07:35,280
correctly highlighted, and the
documentation generator is no

112
00:07:35,280 --> 00:07:39,039
longer mismatching the backticks.
The extra space doesn't detract from

113
00:07:39,039 --> 00:07:43,759
what you're wanting to say, so this is a
pragmatic way to work around the problem.

114
00:07:43,759 --> 00:07:46,479
Alright, so I'm going to finish this
video by mentioning that a function can

115
00:07:46,479 --> 00:07:50,800
include more than one return statement.
So we return a value on line 50 if the

116
00:07:50,800 --> 00:07:54,319
condition's True.
if it isn't, we're returning a different

117
00:07:54,319 --> 00:07:57,759
value on line 60.
It may not have been obvious that

118
00:07:57,760 --> 00:08:00,420
you can do that,
so now you know that you can.

119
00:08:00,420 --> 00:08:04,000
Alright, so moving on in the next video, 
we'll have a look at function annotations

120
00:08:04,000 --> 00:08:09,440
and type hints. See you in the next video.

