1
00:00:05,100 --> 00:00:09,100
So how did you get on with the sentence
challenge, that was in the last video.

2
00:00:09,100 --> 00:00:13,400
Did you manage to finish it? Well the first
step is to create a new string,

3
00:00:13,400 --> 00:00:17,760
containing only the characters that are
either letters or numbers. What I'm going

4
00:00:17,760 --> 00:00:24,300
to do is call up the Python string
methods in the documentation.

5
00:00:24,330 --> 00:00:28,290
We've looked at this documentation back
in the Program Flow Control in Python

6
00:00:28,290 --> 00:00:31,830
section. It's an alphabetical list of all
the methods that the string class

7
00:00:31,830 --> 00:00:37,350
provides. So scrolling down, the two
methods that we're looking are isalnum

8
00:00:37,350 --> 00:00:42,660
and isalpha -
so these two here. If you want to allow

9
00:00:42,660 --> 00:00:47,550
numbers in your palindromes, you'd use the
isalnum method. isalpha will only work

10
00:00:47,550 --> 00:00:52,710
with sentences that don't include digits.
I'm going to use isalnum. It returns

11
00:00:52,710 --> 00:00:57,540
True if all characters are alphanumeric.
We'll be calling this method on a single

12
00:00:57,540 --> 00:01:02,520
character, as we iterate over the string.
The document mentions all characters,

13
00:01:02,520 --> 00:01:06,960
but the string we'll be checking will only
contain one character. Alright so go back

14
00:01:06,960 --> 00:01:12,560
to the code. We're going to add the function
after the is_palindrome function.

15
00:01:12,560 --> 00:01:15,680
Remember that we should put two
break lines after each function, so

16
00:01:15,690 --> 00:01:21,470
therefore I'm going to begin on line 12.
So I'm going to type def palindrome

17
00:01:21,470 --> 00:01:29,610
underscore sentence, in parentheses,
sentence. Outside of the parentheses I'm

18
00:01:29,610 --> 00:01:34,800
going to colon. Put a couple of lines there, which
we need to have between the end of the

19
00:01:34,800 --> 00:01:40,710
method and the start of the other code. I'm gonna
start by typing string equals two

20
00:01:40,710 --> 00:01:50,080
double quotes - so it's an empty string.
Next line, for char in sentence :

21
00:01:50,080 --> 00:02:01,040
I'm going to type if char.isalnum
parentheses : string plus equals char.

22
00:02:01,040 --> 00:02:05,009
Let me call the function we iterate over
the sentence, that's passed as its

23
00:02:05,009 --> 00:02:09,209
argument. If a character is an
alphanumeric character - when isalnum

24
00:02:09,209 --> 00:02:13,280
turns True in other words, then
we'll add that character to the string.

25
00:02:13,280 --> 00:02:17,400
Otherwise, we ignore it and move on to
the next one. When the loop finishes,

26
00:02:17,400 --> 00:02:20,600
we'll have a string containing only the
letters and digits from the original

27
00:02:20,610 --> 00:02:24,870
sentence. Alright, so that's the first
step completed. We've produced a string

28
00:02:24,870 --> 00:02:28,960
that will only contain letters and
digits. If you've used isalpha instead,

29
00:02:28,960 --> 00:02:33,020
then your string won't contain numbers. The
next bit of code is easy because we've

30
00:02:33,030 --> 00:02:36,930
already written it. We want to check if
this new string is a palindrome, and we've

31
00:02:36,930 --> 00:02:42,020
already got the code to do that, in our
is_palindrome function on line six.

32
00:02:42,020 --> 00:02:46,560
Now what we should do, is just call is
palindrome, but I haven't explained about

33
00:02:46,560 --> 00:02:48,480
calling one function from another one yet,

34
00:02:48,480 --> 00:02:52,019
so we'll do it the long way to start
with. So we're going to copy the code

35
00:02:52,020 --> 00:02:56,560
from is_palindrome and paste it into
our palindrome_sentence function.

36
00:02:56,560 --> 00:03:00,370
I'm gonna copy this one line here,
and we're going to paste it down here

37
00:03:00,370 --> 00:03:05,349
on line 18, indented at the same level as before.

38
00:03:05,349 --> 00:03:10,400
Two line breaks there - two blank lines -
and I'm also going to move it up a line

39
00:03:10,400 --> 00:03:13,080
because we only need the two blank
lines after the end of the function

40
00:03:13,090 --> 00:03:17,349
there, before the start of the other code.
And I am going to talk more about

41
00:03:17,349 --> 00:03:22,540
this in the next video. One reason we set
this challenge, is to lead into functions

42
00:03:22,540 --> 00:03:26,290
that call other functions, but with what
we've covered so far, this is probably

43
00:03:26,290 --> 00:03:30,099
how you'll have solved the challenge.
Remember to have two blank lines after

44
00:03:30,100 --> 00:03:34,240
the function, which I've already done.
Alright, so let's see if this works.

45
00:03:34,240 --> 00:03:38,200
We're going to change our main code now, to
call this new function. At the moment, you

46
00:03:38,200 --> 00:03:43,510
can see down here, on line 22. So what I'm gonna
do is change line 22. Instead of calling

47
00:03:43,510 --> 00:03:52,900
is_palindrome, we're going to call our 
palindrome_sentence function, like so.

48
00:03:52,900 --> 00:03:57,780
Alright, so at this point now, we've
done that, let's run it. I'm going to

49
00:03:57,790 --> 00:04:04,060
paste in a sentence, Was it a car or a
cat I saw, like so. Press ENTER. Was it a

50
00:04:04,060 --> 00:04:09,180
cat or a car I saw, is a palindrome. Now It
should still work with single words.

51
00:04:09,180 --> 00:04:15,260
Let's run it again, and this time I'm
going to enter Radar with a capital R.

52
00:04:15,260 --> 00:04:20,320
That still works as well, and just run a quick test -

53
00:04:20,320 --> 00:04:26,700
Python isn't a palindrome.
And again, remember to test your code

54
00:04:26,710 --> 00:04:30,220
with words and sentences that aren't
palindromes, to make sure you get the

55
00:04:30,220 --> 00:04:35,050
correct results, or rather, do further
testing with multiple palindromes, non-

56
00:04:35,050 --> 00:04:39,670
palindrome_sentences, to make sure it
works. Now a useful tip when writing more

57
00:04:39,670 --> 00:04:44,230
complex code, is to print things out.
That's often quicker than using the

58
00:04:44,230 --> 00:04:48,610
debugger to find problems. In our
palindrome_sentence function, you can

59
00:04:48,610 --> 00:04:52,780
check that the first step is working, by
printing out the value of string.

60
00:04:52,780 --> 00:04:56,320
Let's have a go doing that, and  
we'll put the code right before the return

61
00:04:56,320 --> 00:05:01,020
statement. So I'm going to add some code down here. 
Over here, I'm going to type print

62
00:05:01,020 --> 00:05:06,960
string, in parentheses. Alright, so let's run it again.

63
00:05:08,420 --> 00:05:15,090
This time I'll enter do geese see god?

64
00:05:15,090 --> 00:05:18,930
You see we get dogeeseseegod printed
out, which is a quick way to check that

65
00:05:18,930 --> 00:05:22,280
the first part of the function is working correctly.

66
00:05:22,280 --> 00:05:26,440
Alright, so test the function thoroughly,
and we'll make some improvements to it,

67
00:05:26,440 --> 00:05:29,240
in the next video.

