1
00:00:04,680 --> 00:00:09,240
Alright so continuing with our code. So
far we've tested this code with an item

2
00:00:09,240 --> 00:00:14,740
that does exist in the list. Let's now
see what happens if that item doesn't exist.

3
00:00:14,740 --> 00:00:16,680
So what I'm going to do is change line three.

4
00:00:16,680 --> 00:00:21,980
Instead of searching for spam,
I'm going to search for albatross.

5
00:00:23,020 --> 00:00:30,180
Alright, we're gonna run the program now,
and you can see item found at position None.

6
00:00:30,180 --> 00:00:34,560
Now that message is a bit misleading - the item
wasn't found at all - but you can see that

7
00:00:34,560 --> 00:00:39,600
None is coming from line four in our
code. We initialized found_at to None

8
00:00:39,600 --> 00:00:44,940
on that line. Because albatross wasn't
found, line nine of our code wasn't

9
00:00:44,940 --> 00:00:49,770
executed and found_at wasn't 
given another value. It's very important

10
00:00:49,770 --> 00:00:53,490
that we bound found_at to
something on line four. So let's see what

11
00:00:53,490 --> 00:00:58,500
would have happened if we hadn't have
done that. I'm gonna comment out line four.

12
00:00:58,500 --> 00:01:04,760
We'll run the program again. This time
you can see we've got an error, and the error,

13
00:01:04,760 --> 00:01:09,320
right over at the right hand side
there, line 12. And on line 12 we're

14
00:01:09,320 --> 00:01:13,580
trying to print the value of found_at
but it hasn't been given a value,

15
00:01:13,580 --> 00:01:17,180
and in fact my IDE has highlighted
found_at on line 12,

16
00:01:17,180 --> 00:01:22,540
if we hover over it, name found_at
can be not defined. So that's an error.

17
00:01:22,540 --> 00:01:27,120
Now the code would work if albatross was
found, because found_at would

18
00:01:27,120 --> 00:01:33,460
have been attached to a value on line 9.
I'd better uncomment that line to fix that error.

19
00:01:33,460 --> 00:01:37,720
So that explains why we had to
initialize found_at,

20
00:01:37,720 --> 00:01:42,600
but what's this None that I initialized it
to? Well None is a constant that

21
00:01:42,600 --> 00:01:47,220
represents nothing. found_at
now exists but it doesn't yet have a

22
00:01:47,220 --> 00:01:51,299
value when the code on line 4 is
executed. One thing we can use it for

23
00:01:51,299 --> 00:01:56,070
here, is to improve the message we get.
Let's have a go at doing that, and we can

24
00:01:56,070 --> 00:01:59,960
change the code. So on line 12, what I'm
going to do is add some code there:

25
00:01:59,960 --> 00:02:10,039
if found_at is not None :
then we'll indent the next line.

26
00:02:10,039 --> 00:02:15,920
Then we'll put an else to the left-hand
margin print parentheses double quotes

27
00:02:15,920 --> 00:02:22,960
left and right curly braces
not found, and our double quote dot

28
00:02:22,960 --> 00:02:31,080
format parentheses item_to_find.
And now that we've done that,

29
00:02:31,080 --> 00:02:34,480
we run the program,
we get a better message when an item is

30
00:02:34,480 --> 00:02:40,280
not found, as you can see here. So looking
at the code, we've initialized found_at

31
00:02:40,280 --> 00:02:44,340
to None on line four. If
it's still None when we get to line 12,

32
00:02:44,340 --> 00:02:47,620
then we didn't find the item we're
looking for. Alright I want to finish

33
00:02:47,620 --> 00:02:52,020
this video now with an apology.
We shouldn't write code like that for loop,

34
00:02:52,020 --> 00:02:56,800
on lines seven through ten. Python
is an incredibly rich language and one

35
00:02:56,800 --> 00:03:00,580
of the reasons that it's so successful,
is because it comes with so much

36
00:03:00,580 --> 00:03:04,990
included as standard. We've only looked
at a small part of the language so far,

37
00:03:04,990 --> 00:03:09,670
and the examples I use have to reflect
that. Even then, I've had to introduce the

38
00:03:09,670 --> 00:03:14,709
list type for this example. The code
demonstrated how to use break and why

39
00:03:14,709 --> 00:03:19,120
you'd want to, but there's a much easier
way to write this code. So what I'm going

40
00:03:19,120 --> 00:03:23,190
to do, so I'm going to comment out the
for loop

41
00:03:25,560 --> 00:03:35,100
if item_to_find is
in shopping_list colon

42
00:03:35,100 --> 00:03:42,960
found_at is equal to shopping_list.index.
Then in parentheses,

43
00:03:42,960 --> 00:03:47,840
item_to_find, then we've
got a closing parenthesis there.

44
00:03:47,840 --> 00:03:53,680
Run the program now, and you see we've got the
same result there. And just to confirm that it

45
00:03:53,680 --> 00:03:58,420
is working, if I change albatross back
now to spam, which it originally was at

46
00:03:58,420 --> 00:04:05,880
the start of the video. Run this, item
found at position three. So that also works.

47
00:04:05,880 --> 00:04:09,580
As I said, Python is a very
rich language. It's got all sorts of

48
00:04:09,580 --> 00:04:13,180
things included and we'll be seeing most
of them in this course, and you'll learn

49
00:04:13,180 --> 00:04:18,040
what line thirteen is doing in the next
section of this course. I'll also explain

50
00:04:18,040 --> 00:04:22,210
why lines twelve and thirteen aren't
very efficient and how we can improve

51
00:04:22,210 --> 00:04:26,080
that code. Before that though, in the next
video, we'll look at another type of loop -

52
00:04:26,080 --> 00:04:30,750
the while loop.
See you in the next video.

