1
00:00:04,460 --> 00:00:09,510
So the challenge that I mentioned in the
last video was to modify the program, our

2
00:00:09,510 --> 00:00:13,720
guessing game, using a while loop to
allow the player to continue guessing.

3
00:00:13,720 --> 00:00:18,240
The important change is the use of a
while loop. As long as the guess doesn't

4
00:00:18,240 --> 00:00:22,470
equal the answer, the loop will continue
requesting a new guess and checking

5
00:00:22,470 --> 00:00:26,310
the answer.  So I'm going to start by
wrapping all the code, including the

6
00:00:26,310 --> 00:00:31,380
input line, in a while loop. I'm basically,
going to come up to the guess equals int

7
00:00:31,380 --> 00:00:37,280
input line. Press over here, then I press
ENTER there and I'm going to type while

8
00:00:37,280 --> 00:00:45,980
guess is not equal to answer : and
then I'm going to indent all this code,

9
00:00:45,980 --> 00:00:50,460
right down to the end of the file. So I
can just select it all and press tab,

10
00:00:50,460 --> 00:00:54,980
and it all gets indented, as you can see.
And actually, what I'll do is, in terms of

11
00:00:54,989 --> 00:00:58,829
the print, let's change that as well.
I'm going to cut that out and I'm

12
00:00:58,829 --> 00:01:03,449
going to put that on line six. We don't
need to re-show that every time around

13
00:01:03,449 --> 00:01:08,220
the loop. Alright so the way we've got
our code now, it's going to keep looping

14
00:01:08,220 --> 00:01:13,079
until guess equals answer. So in other words,
when guess equals answer, the loop

15
00:01:13,079 --> 00:01:15,600
will be exited.
So we've got a bit of tidying up to do,

16
00:01:15,600 --> 00:01:21,390
starting with the warning on line
eight, and if we hover over that: name

17
00:01:21,390 --> 00:01:26,009
guess can be not defined. So guess hasn't
been given a value yet, which means

18
00:01:26,009 --> 00:01:30,270
we can't use its value in the condition.
So we can fix that by initializing

19
00:01:30,270 --> 00:01:37,700
guess. Let's do that after printing
the answer - guess is equal to zero.

20
00:01:37,700 --> 00:01:42,509
Basically here, we want to initialize it
to any number. I'll make a point of that,

21
00:01:42,509 --> 00:01:51,930
a note of that, to any number that doesn't
equal the answer. Okay. So this is

22
00:01:51,930 --> 00:01:55,079
working because we're not allowing zero
as a valid answer, so it's probably a good

23
00:01:55,079 --> 00:02:00,119
value to use here. Alright so what next?
Well let's look on line 12. We're

24
00:02:00,120 --> 00:02:04,380
checking to see if the answer's correct.
Now that we're allowing many guesses,

25
00:02:04,380 --> 00:02:07,649
we're going to be doing that check each
time around the loop, and that means we

26
00:02:07,649 --> 00:02:11,038
don't know if they got the correct
answer on the first guess, and therefore

27
00:02:11,038 --> 00:02:14,910
the message is currently wrong. So what
I'm going to do is copy the text from

28
00:02:14,910 --> 00:02:20,489
line 21 and replace it on
line 13. So come down here. I'm gonna

29
00:02:20,489 --> 00:02:23,730
copy that line. I could copy the text or
the whole thing. I'll just copy the whole

30
00:02:23,730 --> 00:02:31,300
line and put it there, like so. Alright, now
the other thing we need to do here, is if

31
00:02:31,300 --> 00:02:35,459
they have got the correct answer, we
don't want to loop again. A break will

32
00:02:35,460 --> 00:02:40,380
break out of the loop, so let's add that
after the print of Well done you guessed it,

33
00:02:40,380 --> 00:02:45,080
like so. Alright, so we're almost
done at this point. Now we're getting the

34
00:02:45,090 --> 00:02:50,640
players input on line 10. That means we
don't want to do it again, down here on

35
00:02:50,640 --> 00:02:56,500
line 20. In fact, all the code from line
20 onwards, can effectively be deleted.

36
00:02:56,500 --> 00:03:02,609
What I'll do now is I'll just comment
that out, and essentially that's our

37
00:03:02,609 --> 00:03:06,620
solution, and basically, the code is
complete.

38
00:03:06,620 --> 00:03:10,060
So let's actually test it, though, and see
if it works. What we're going to do is,

39
00:03:10,079 --> 00:03:12,930
I'm going to run the program and confirm
that it keeps requesting guesses until

40
00:03:12,930 --> 00:03:19,660
we get the correct answer. Alright so I know
the answer's four - I'm just going to type 7,

41
00:03:19,660 --> 00:03:28,440
Please guess lower, 2, higher, 3.
Obviously it's working, and 4, Well done

42
00:03:28,440 --> 00:03:32,040
you've guessed it. So clearly that's
working. So that's the solution, or our

43
00:03:32,040 --> 00:03:38,520
solution. If your solution behaves in the
same way, then well done.

44
00:03:38,520 --> 00:03:42,300
Now you may have written the code
differently. There's often several ways

45
00:03:42,300 --> 00:03:45,960
to get the same results in almost any
programming code, you care to come up

46
00:03:45,960 --> 00:03:49,980
with, so don't be worried if your code
looks different to ours. And if you

47
00:03:49,980 --> 00:03:54,060
struggled to complete the challenge, also
don't worry - the important thing here was

48
00:03:54,060 --> 00:03:57,420
to have a go. You'll find that my
solution makes more sense, if you tried

49
00:03:57,420 --> 00:04:01,380
to solve the challenge yourself, first.
More importantly, perhaps you'll also

50
00:04:01,380 --> 00:04:06,300
understand why, what you were trying,
didn't work.

51
00:04:06,300 --> 00:04:09,450
Alright, now if you recall we had an
optional extra part of the challenge,

52
00:04:09,450 --> 00:04:14,640
to check for zero, and break out of the loop
if zero was typed. Now we can do that by

53
00:04:14,640 --> 00:04:18,630
adding an if block, after getting the
players input. Let's have a go of doing

54
00:04:18,630 --> 00:04:23,060
that. So I'm gonna put that on line 12,
before the test for the correct answer,

55
00:04:23,060 --> 00:04:31,560
and here I can go up and type if guess
is equal to zero : break. Alright, that's

56
00:04:31,560 --> 00:04:35,490
the code. So that gives the player a way
to get out of the program, if they can't

57
00:04:35,490 --> 00:04:39,360
guess the number. Now before testing that,
I'm gonna increase the range of numbers

58
00:04:39,360 --> 00:04:42,930
they have to guess. Let's just test that
as well, and instead of making it 10, as

59
00:04:42,930 --> 00:04:49,260
it currently is, I'm going to make that
1000. Run the program. Let's enter a few

60
00:04:49,260 --> 00:04:58,280
guesses and we know what the correct answer is
- we'll say 500, Please guess higher, 750,

61
00:04:58,280 --> 00:05:05,880
Please guess lower, 625. Okay that's enough.
Let's enter zero, and we can see we've successfully

62
00:05:05,880 --> 00:05:09,270
exited the program when I entered zero.
Alright so that's the challenge

63
00:05:09,270 --> 00:05:13,440
completed, including the optional extra
part as well. Now by the way, you should

64
00:05:13,440 --> 00:05:17,220
be able to guess the correct number
within ten guesses. Now if you're not

65
00:05:17,220 --> 00:05:21,210
sure how you can do that, I did allude
to it a little bit by the types of the

66
00:05:21,210 --> 00:05:27,500
numbers that I was entering, but full
details will be disclosed in the next video.

