1
00:00:04,860 --> 00:00:08,610
Now that we've seen what a function is, and
how to write them, let's practice writing

2
00:01:42,360 --> 00:01:46,060
Alright, so I'm going to call the new function is_palindrome,

3
00:01:46,060 --> 00:01:49,160
and we're going to add it after our multiply function,

4
00:01:49,160 --> 00:01:54,120
down here. Now remember to leave two
blank lines after multiply. Functions

5
00:01:54,120 --> 00:01:58,530
are separated from each other with two
blank lines. I'm going to start typing on

6
00:01:58,530 --> 00:02:03,650
line six; def is_palindrome

7
00:02:03,920 --> 00:02:12,220
parentheses string and then a colon
outside the parentheses, and press ENTER there.

8
00:02:12,220 --> 00:02:16,320
IntelliJ has automatically
indented the next line, because it knows

9
00:02:16,320 --> 00:02:20,760
that we're about to type a new block of
code. Note that we've now got an error in

10
00:02:20,760 --> 00:02:24,080
the main part of the code, on line eight.
That's normal.

11
00:02:24,080 --> 00:02:28,380
A function definition must be followed
by an indented block of code, and we

12
00:02:28,380 --> 00:02:32,540
haven't written that yet. IntelliJ is
telling us that an indent is expected,

13
00:02:32,540 --> 00:02:37,060
and that can be confusing when you first
start writing functions. Don't worry though,

14
00:02:37,060 --> 00:02:41,280
the error will be fixed as soon
as we enter the body of our function

15
00:02:41,280 --> 00:02:45,420
Okay, so to check if a word is a
palindrome, we can just reverse it and

16
00:02:45,420 --> 00:02:49,890
check if it's equal to the original word.
So I'm going to shorten this code in a

17
00:02:49,890 --> 00:02:54,000
moment. For now I'll write it in a
slightly longer way, to make it clearer

18
00:02:54,000 --> 00:03:02,200
what we're doing. So on line seven I'm going
to type backwards is equal to string

19
00:03:02,200 --> 00:03:08,160
square brackets, and in the square
brackets, colon colon minus one, and on

20
00:03:08,160 --> 00:03:18,300
the next line, I'm going to type return
backwards equal equals, or equal to string.

21
00:03:18,300 --> 00:03:19,600
Then I'm going to leave two black lines

22
00:03:19,610 --> 00:03:24,140
after the end of our function as well. So
hopefully, the code that I've added should

23
00:03:24,140 --> 00:03:29,600
be easy to understand. The slice on line
seven reverses the original string.

24
00:03:29,600 --> 00:03:34,680
On line eight, we check if the reverse
string is the same as the original. If it is,

25
00:03:34,680 --> 00:03:41,000
we return True, otherwise, we return
False. The expression backwards equals

26
00:03:41,000 --> 00:03:45,200
equal string will evaluate to either
True or False, and that's the result that

27
00:03:45,200 --> 00:03:49,160
the function will return. In fact, we
could do all that with one line of code.

28
00:03:49,160 --> 00:03:51,710
You'll often find this written in a more
concise way,

29
00:03:51,710 --> 00:03:55,610
so let's comment out lines seven and
eight. Instead what I'm going to do on

30
00:03:55,610 --> 00:04:02,990
line nine, is type return string square
brackets, and within the square brackets,

31
00:04:02,990 --> 00:04:08,360
colon colon minus one again, and
outside the square brackets, equals

32
00:04:08,360 --> 00:04:16,120
equals string, and making sure there's
two blank lines at the end of our function.

33
00:04:16,120 --> 00:04:19,920
So that's more concise and more
like real code, but when you're learning,

34
00:04:19,920 --> 00:04:23,560
don't worry about splitting lines up
into several parts - it's more important

35
00:04:23,560 --> 00:04:28,880
to write code that you understand, rather
than writing the shortest code possible.

36
00:04:28,880 --> 00:04:33,380
If you find lines seven and eight easier
to understand than line nine at this stage,

37
00:04:33,380 --> 00:04:37,340
then that's fine. Your code will
naturally get more concise, as you get

38
00:04:37,340 --> 00:04:41,630
more comfortable and more experienced in
programming. Alright, so let's see if

39
00:04:41,630 --> 00:04:47,640
this works. I'm going to delete all the
code from line twelve and onwards.

40
00:04:47,640 --> 00:04:51,240
Now that I've done that we need to write
some code to test this new function.

41
00:04:51,240 --> 00:04:58,760
On line 12 I'm going to type word equals
input parentheses double quotes,

42
00:04:58,760 --> 00:05:06,000
Please enter a word to check. We'll add a
colon and a space there as well.

43
00:05:06,000 --> 00:05:12,260
Next line, we're going to do a test; if 
is_palindrome parentheses and

44
00:05:12,260 --> 00:05:18,220
pass word as an argument, and a colon.
The next line we're going to do a print

45
00:05:18,220 --> 00:05:22,280
parentheses double quotes.
Then I'm going to add a single quote and

46
00:05:22,280 --> 00:05:26,390
add our replacement fields, left and right
curly braces and another single quote,

47
00:05:26,390 --> 00:05:32,960
is a palindrome double quote dot format word,

48
00:05:32,960 --> 00:05:38,180
and the two right parentheses. And on line 15
I'm going to type else : Then we need to

49
00:05:38,180 --> 00:05:42,590
print a message for where it's not a
palindrome, so print parentheses double

50
00:05:42,590 --> 00:05:47,240
quotes single quote, our replacement
fields, left right curly braces, single

51
00:05:47,240 --> 00:05:55,220
quote is not a palindrome. We want a
double quote again, dot format and

52
00:05:55,220 --> 00:05:59,640
word in parentheses, and close off the line like so

53
00:05:59,640 --> 00:06:03,400
Alright, so now that we've done that, let's run
the program

54
00:06:03,400 --> 00:06:08,200
and see if it works. So I'm going to click
into the run window and I'm going to

55
00:06:08,200 --> 00:06:15,310
enter racecar, racecar, racecar is a
palindrome, and our function's returning

56
00:06:15,310 --> 00:06:18,729
True, which is what we want to see. So
test the program with a few more words to

57
00:06:18,729 --> 00:06:22,530
be confident that it works. Just to be
sure

58
00:06:22,530 --> 00:06:29,300
I'll enter a word that isn't a palindrome, 
Python, Python is not a palindrome.

59
00:06:29,300 --> 00:06:33,560
It seems to be working, well
sort of. What happens if I enter radar

60
00:06:33,560 --> 00:06:37,920
with a capital R. Let's try that and see
what happens.

61
00:06:39,220 --> 00:06:43,940
That's not good. Palindromes shouldn't
consider the case of the letters.

62
00:06:43,940 --> 00:06:48,560
We had better fix that,
which sounds like a good challenge.

63
00:07:06,380 --> 00:07:09,860
So that's the challenge - see how you go
with it. I'll go over my solution to it

64
00:07:09,860 --> 00:07:12,660
in the next video.

