1
00:00:04,710 --> 00:00:10,500
So just like with the for-loop, we can
use break and continue to break out of a

2
00:00:10,500 --> 00:00:15,520
while loop, or cause it to stop the current
iteration and test the condition again.

3
00:00:15,520 --> 00:00:19,520
Now in our adventure.py example,
break statement can provide a

4
00:00:19,529 --> 00:00:24,150
simple way to quit, without choosing a
valid exit. We can allow the player to

5
00:00:24,150 --> 00:00:29,880
type quit to exit the game. So let's add some
code to achieve that. I'm going to type

6
00:00:29,880 --> 00:00:35,910
if chosen_exit is equal to quit in

7
00:00:35,910 --> 00:00:41,460
double quotes colon. I'll print a message
to the effect that it's game over, so

8
00:00:41,460 --> 00:00:45,500
print parentheses double quotes
Game over.

9
00:00:45,500 --> 00:00:53,300
Then on the next line, break. Alright so
we'll run the game again now, or program.

10
00:00:53,309 --> 00:00:56,760
We can't really say it's a game at this
stage. Alright so I'm going to enter up

11
00:00:56,760 --> 00:01:01,680
when prompted, as you can see on the
screen. That's not in the list and the

12
00:01:01,680 --> 00:01:07,740
loop goes round again. Down will obviously do
the same. If I now enter quit, we get into

13
00:01:07,740 --> 00:01:12,810
that inner if block, so I'll do that. You
can see we've got Game over and it breaks out of

14
00:01:12,810 --> 00:01:16,380
the while loop, and that causes the last
message also to be printed and the

15
00:01:16,380 --> 00:01:21,119
program terminates. So we probably don't
want that last message, aren't you glad

16
00:01:21,119 --> 00:01:25,470
you got out of here? to appear in this
scenario. I'll show you a way to prevent it

17
00:01:25,470 --> 00:01:29,790
at the end of this section. Before that,
I want to finish this video by making our

18
00:01:29,790 --> 00:01:35,920
code a bit easier to use. We've seen the
casefold function in an earlier video.

19
00:01:35,920 --> 00:01:39,960
It produces a lowercase version of a
string and handles languages that have

20
00:01:39,960 --> 00:01:44,880
multiple versions of the same lowercase
character. This would be a good program

21
00:01:44,880 --> 00:01:49,420
to use it in. First though, let's see the
problem we're trying to solve.

22
00:01:49,420 --> 00:01:55,360
So I'm going to run the program again. This time
we're going to enter quit with a capital Q.

23
00:01:55,360 --> 00:01:59,720
Now other loop goes around again, as you
can see here. Because string comparisons

24
00:01:59,729 --> 00:02:03,270
are case sensitive,
quit with a capital Q doesn't match the

25
00:02:03,270 --> 00:02:08,189
string we're checking for on line six. So
this isn't an ideal experience for the

26
00:02:08,189 --> 00:02:12,480
user. We'll just leave them confused and
frustrated and wondering why quit with a

27
00:02:12,480 --> 00:02:16,799
capital Q doesn't work. To fix the
problem, we can convert chosen underscore

28
00:02:16,800 --> 00:02:21,160
exit to lowercase,
before performing the conversion.

29
00:02:21,160 --> 00:02:26,540
So what I can do is come over here and put a dot
after the chosen underscore exit name on line

30
00:02:26,550 --> 00:02:31,590
six, dot casefold with a left and
right parenthesis, and now when I run the

31
00:02:31,590 --> 00:02:40,890
program again - incidentally if you run a
program when it's already running, then

32
00:02:40,890 --> 00:02:44,880
you'll get this scenario pop up. And this is
popping up because the previous code was

33
00:02:44,880 --> 00:02:48,330
still running and I didn't stop it.
I didn't quit. That's why I'm getting this

34
00:02:48,330 --> 00:02:52,050
dialog, and IntelliJ prevents you from
running the same program while it's

35
00:02:52,050 --> 00:02:56,190
already running, which is probably safest
when learning. So when you see this

36
00:02:56,190 --> 00:03:00,040
dialog, that's what it means. The program
is still running from last time.

37
00:03:00,040 --> 00:03:04,980
So click on stop and rerun to terminate
the previous run and run our latest code.

38
00:03:04,980 --> 00:03:07,710
I'm gonna do that now. The other
alternative is you could have just

39
00:03:07,710 --> 00:03:11,640
clicked on the red stop button, to the
left down the bottom or the top right,

40
00:03:11,640 --> 00:03:16,280
before you ran. That would also fix the
problem, enabling you to run

41
00:03:16,290 --> 00:03:20,250
the program. But I'm gonna stop and rerun.
Now I'm going to enter quit with a

42
00:03:20,250 --> 00:03:26,640
capital Q. This time the code breaks out
of the loop. So break does the same thing

43
00:03:26,640 --> 00:03:30,810
with a while loop as it does with for -
breaks out of the loop it appears in,

44
00:03:30,810 --> 00:03:35,850
causing execution to continue with the
next line of code, after the loop. So in

45
00:03:35,850 --> 00:03:40,710
our example, that's line ten. We'll see
another example of using break with a

46
00:03:40,710 --> 00:03:44,640
while loop in the next couple of videos.
We're going to make our guessing game a

47
00:03:44,640 --> 00:03:49,160
bit more interesting.
See you in the next video.

