1
00:00:06,390 --> 00:00:09,060
Now, unlike some other programming languages,

2
00:00:09,090 --> 00:00:13,410
if you've come from say C++ or Java,

3
00:00:14,160 --> 00:00:17,220
there is no such thing as block scope in Python.

4
00:00:17,670 --> 00:00:21,690
What this means is that if you were to create an if statement,

5
00:00:22,290 --> 00:00:25,140
say if 3 > 2,

6
00:00:25,890 --> 00:00:30,890
and if you were to create a new variable inside an if block or a while loop or a

7
00:00:31,680 --> 00:00:35,460
for loop, basically any sort of block of code that's indented,

8
00:00:35,880 --> 00:00:38,130
this does not count as offence.

9
00:00:38,640 --> 00:00:43,110
It still has the same scope as its enclosing function,

10
00:00:43,410 --> 00:00:46,560
or if there is no enclosing function then it has global scope.

11
00:00:47,100 --> 00:00:51,120
So let me show you a full example. Let's say we had a list of enemies,

12
00:00:51,150 --> 00:00:56,010
so the enemies could be skeletons, zombies, or aliens. So now,

13
00:00:56,010 --> 00:00:58,980
if I was to define a game level, right?

14
00:00:59,010 --> 00:01:01,710
Like the level that the user is currently playing at,

15
00:01:01,740 --> 00:01:03,240
let's say they're on level 3.

16
00:01:03,930 --> 00:01:08,930
And I create an if statement and I check if the game_level is less than level

17
00:01:09,570 --> 00:01:12,990
5, well, in that case, I want to create a new enemy,

18
00:01:13,470 --> 00:01:16,560
but I don't want the enemy to be too difficult to beat.

19
00:01:17,100 --> 00:01:21,450
So I'm going to pick from the list of enemies and I'm going to pick the first

20
00:01:21,450 --> 00:01:23,520
one. Notice how,

21
00:01:23,550 --> 00:01:28,470
even though this new enemy is a variable that's created within this

22
00:01:28,530 --> 00:01:31,800
if block, if I go outside the if block

23
00:01:31,860 --> 00:01:36,840
so I'm not indented at all anymore, and I try to print this new enemy,

24
00:01:38,010 --> 00:01:41,070
this is perfectly valid code. And if I run the code,

25
00:01:41,130 --> 00:01:43,230
you'll see skeleton being printed.

26
00:01:44,070 --> 00:01:49,070
But notice how as soon as I embed this within a function,

27
00:01:50,220 --> 00:01:52,320
so let's define a new function.

28
00:01:53,670 --> 00:01:58,670
And now this line error is out because within the function there is local scope.

29
00:01:59,580 --> 00:02:03,840
So now this new enemy is available anywhere within this function,

30
00:02:04,110 --> 00:02:09,110
because blocks like if, while, for, all of these blocks of code with colons and

31
00:02:09,960 --> 00:02:13,440
indentation, they don't count as creating a local scope.

32
00:02:13,860 --> 00:02:16,470
So in order to print this new enemy,

33
00:02:16,830 --> 00:02:20,490
I actually have to be within the boundary of this function,

34
00:02:20,580 --> 00:02:22,710
which means my code has to be here.

35
00:02:24,120 --> 00:02:29,070
The most important thing to remember from this is if you create a variable

36
00:02:29,430 --> 00:02:34,320
within a function, then it's only available within that function.

37
00:02:35,160 --> 00:02:40,160
But if you create a variable within an if block or a while loop or a for loop or

38
00:02:42,060 --> 00:02:44,730
anything that has the indentation and the colon,

39
00:02:45,150 --> 00:02:49,050
then that does not count as creating a separate local scope.

