1
00:00:05,300 --> 00:00:09,430
so in the previous we talked
about the While loop and we had a very simple

2
00:00:09,430 --> 00:00:13,410
game that we modified to use a random now the problems we had with that

3
00:00:13,410 --> 00:00:18,210
code of course was that were only able
to have two guess’ in the code and

4
00:00:18,210 --> 00:00:22,280
the consequently it made the game very frustrating because unless you fluked it

5
00:00:22,280 --> 00:00:26,590
in which I tried three times and was
unable to do that, you wouldn’t be able to solve it

6
00:00:26,590 --> 00:00:29,790
so you won’t be able to win the game

7
00:00:29,790 --> 00:00:34,050
so the challenge I got the same code I pasted in a new Python file called

8
00:00:34,050 --> 00:00:38,930
challenge, and your challenge is to
modify the program below and to use a

9
00:00:38,930 --> 00:00:42,950
while loop to allow as many guesses as necessary
so you see obviously in the current code

10
00:00:42,950 --> 00:00:47,550
at the moment with only have two guess’ and that's it but
your gonna change all so the program

11
00:00:47,550 --> 00:00:51,530
now should let the player know where to guess
higher or lower and should put a message

12
00:00:51,530 --> 00:00:55,950
whether the guess is correct and the correct guess will terminate the program, now as an optional extra

13
00:00:55,950 --> 00:01:01,230
allow the player to quit by entering the number 0 before they guess

14
00:01:01,230 --> 00:01:04,330
so time to pause the video go away and see if you can figure this out, and of course were

15
00:01:04,330 --> 00:01:09,050
you want to create a while loop statement to do that

16
00:01:09,050 --> 00:01:12,760
and when you're ready to come back and
see the solution come back and will go through that

17
00:01:12,760 --> 00:01:20,700
so pause the video now, so hopefully you managed to get a
solution to that and to get it working

18
00:01:20,700 --> 00:01:25,620
so lets go through our proposed solution
now to see whether we can figure it out

19
00:01:25,620 --> 00:01:28,870
so what we need to do is we need to put
a while loop in here we need to get rid of all

20
00:01:28,870 --> 00:01:33,780
these extra bits of code that we no
longer need so were gonna leave the first part which

21
00:01:33,780 --> 00:01:34,660
is correct

22
00:01:34,660 --> 00:01:38,910
which is generating the random number and printing it out on the screen and what we're

23
00:01:38,910 --> 00:01:43,590
going to do now is instead of putting guess = int(input) input what we need to do is

24
00:01:43,590 --> 00:01:49,260
initialized that to 0 because if you recall in our
discussions with a while loop we need to

25
00:01:49,260 --> 00:01:54,520
pre-initialized that variable as we done in the for loop, a for loop doesn't need to but a while it

26
00:01:54,520 --> 00:01:56,830
does, so its going to be

27
00:01:56,830 --> 00:02:07,990
that can be initialised basically to any number, it has to be outside of valid
range again, if we got it in the valid

28
00:02:07,990 --> 00:02:13,550
range as you saw when I created in the
previous video then the while would not

29
00:02:13,550 --> 00:02:17,470
be started for the first time so making
it zero when its going to be OK

30
00:02:17,470 --> 00:02:22,560
and because the reason we know that it's not
a valid number is of course on line 15 we

31
00:02:22,560 --> 00:02:26,530
are getting a random number which starts on the
low range as one and in the highest ranges

32
00:02:26,530 --> 00:02:31,100
which is tent and therefore we know that we get a
number of 21 and 10 so guess has to be an invalid

33
00:02:31,100 --> 00:02:43,300
number, so to fix this now we need to change that to while loop so we
gonna

34
00:02:43,300 --> 00:02:52,490
and we are gonna put, and so again because guess is 0 this code will work on the first time we run it and

35
00:02:52,490 --> 00:02:56,530
will prompt for input which is correct
and then we still need to put some tests in here

36
00:02:56,530 --> 00:03:07,220
to check whether it's less than or greater than or put an else in there if you got it the first time, so were going to delete that, and get rid of this “you got it right

37
00:03:07,220 --> 00:03:12,280
the first time” so we’ve now gotta to check if guess is less than
the answer in other words I’ve typed in

38
00:03:12,280 --> 00:03:16,630
a number that is lower than the answers, so its gonna
say please guess higher, otherwise have typed in a

39
00:03:16,630 --> 00:03:22,320
number that's higher than the actual answer so the else comes in and says we need to guess lower, we need to take

40
00:03:22,320 --> 00:03:25,470
this next line actually out because we don't wanna input the

41
00:03:25,470 --> 00:03:33,810
code again what we really want to do is
wanna put an else there, and I should

42
00:03:33,810 --> 00:03:37,940
have change this code here to be an elif

43
00:03:37,940 --> 00:03:50,220
now the final else we longer need, so you can see now we are left with a while loop that start making sure

44
00:03:50,220 --> 00:03:54,920
that guess is not equal to answer as soon as
guess equals the loop is effectively going to end

45
00:03:54,920 --> 00:04:01,060
and so what happens is we do is guess = inputs so we are
getting the integer from the keyboard

46
00:04:01,060 --> 00:04:07,360
if the typed number is less than ours were gonna print a message please guess higher, else if guess is

47
00:04:07,360 --> 00:04:13,000
greater than answer, then we print the
message please guess lower than else if it

48
00:04:13,000 --> 00:04:18,390
was not less and greater then in it means the we got it right, so print will done you guess it, and obviously if the

49
00:04:18,390 --> 00:04:21,810
code gets down to hear that's not
true it's gonna look back again until guess

50
00:04:21,810 --> 00:04:23,130
as equal to answer

51
00:04:23,130 --> 00:04:26,210
essentially as long as the guess doesn't
equal to answer the loop’s continues to

52
00:04:26,210 --> 00:04:29,200
request a new guess and checking the
answer and you can see now that the if

53
00:04:29,200 --> 00:04:32,060
statements are obviously a lot easier
because of taking something out we just

54
00:04:32,060 --> 00:04:37,910
checking for less then greater than we can let the player know to guess higher or lower or print when they get it right, so lets just run

55
00:04:37,910 --> 00:04:41,700
that and make sure that it does work, please guess a number between 1 and 10

56
00:04:41,700 --> 00:04:49,220
will try 5, please guess lower so 3 and that confused me for a moment because of course we're running the wrong

57
00:04:49,220 --> 00:04:54,650
program so I need to close that, we are
running the while program we created in

58
00:04:54,650 --> 00:04:58,970
the previous video, I need to right
click and run this first, click challenge

59
00:04:58,970 --> 00:05:08,340
will try again, 5 again please guess higher, try 7 days, please guess higher will try 8 well

60
00:05:08,340 --> 00:05:22,090
done you guess it, we got the number 8, and will just try again so lets try the number 9, guess lower, 1,5,6,7, well done you guessed it, we will try again

61
00:05:22,090 --> 00:05:35,400
10 please guess lower, 5 higher, 7, 8, 9, well done you guessed it, is clearly working now so lets also do the

62
00:05:35,400 --> 00:05:40,250
optional challenge and of course the
optional extra of this challenge is for 0 to

63
00:05:40,250 --> 00:05:45,280
break out of the loop and it's quite
simple to do after the guess where we are

64
00:05:45,280 --> 00:05:49,500
entering the number or getting the
number from the keyboard we put

65
00:05:50,290 --> 00:06:02,250
that should be equal to 0, two == and now if we run that, lets make sure that working, so if we put a 0, and there we go exits the loop, and just to make the app

66
00:06:02,250 --> 00:06:07,190
more interesting so if we close this down let's
make the highest number a 1000 now

67
00:06:07,190 --> 00:06:11,150
what you should be able to do, is you have
to guess the correct number within 10

68
00:06:11,150 --> 00:06:15,040
guesses, if looked at what I was doing
initially probably got an idea what I

69
00:06:15,040 --> 00:06:19,250
was doing especially from the
previous video and little bit in this

70
00:06:19,250 --> 00:06:23,820
video but there is a way of doing this so you can within 10 guesses no matter what the

71
00:06:23,820 --> 00:06:28,430
number is the, whatever the number ranges you should be
able to determine that within 10 guesses

72
00:06:28,430 --> 00:06:32,320
So I’m going to leave that Im not going to show you how to do that, see if you can figure

73
00:06:32,320 --> 00:06:36,820
that out but you will find out how and
why later in the course so that's it

74
00:06:36,820 --> 00:06:40,430
that's this video finished now and will
see you in the next one

