WEBVTT 1 00:00:01.920 --> 00:00:06.050 so in the previous we talked about the While loop and we had a very simple 2 00:00:06.050 --> 00:00:10.030 game that we modified to use a random now the problems we had with that 3 00:00:10.030 --> 00:00:14.830 code of course was that were only able to have two guess’ in the code and 4 00:00:14.830 --> 00:00:18.900 the consequently it made the game very frustrating because unless you fluked it 5 00:00:18.900 --> 00:00:23.210 in which I tried three times and was unable to do that, you wouldn’t be able to solve it 6 00:00:23.210 --> 00:00:26.410 so you won’t be able to win the game 7 00:00:26.410 --> 00:00:30.670 so the challenge I got the same code I pasted in a new Python file called 8 00:00:30.670 --> 00:00:35.550 challenge, and your challenge is to modify the program below and to use a 9 00:00:35.550 --> 00:00:39.579 while loop to allow as many guesses as necessary so you see obviously in the current code 10 00:00:39.579 --> 00:00:44.179 at the moment with only have two guess’ and that's it but your gonna change all so the program 11 00:00:44.179 --> 00:00:48.159 now should let the player know where to guess higher or lower and should put a message 12 00:00:48.159 --> 00:00:52.579 whether the guess is correct and the correct guess will terminate the program, now as an optional extra 13 00:00:52.579 --> 00:00:57.850 allow the player to quit by entering the number 0 before they guess 14 00:00:57.850 --> 00:01:00.959 so time to pause the video go away and see if you can figure this out, and of course were 15 00:01:00.959 --> 00:01:05.670 you want to create a while loop statement to do that 16 00:01:05.670 --> 00:01:09.380 and when you're ready to come back and see the solution come back and will go through that 17 00:01:09.380 --> 00:01:17.329 so pause the video now, so hopefully you managed to get a solution to that and to get it working 18 00:01:17.329 --> 00:01:22.240 so lets go through our proposed solution now to see whether we can figure it out 19 00:01:22.240 --> 00:01:25.490 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:25.490 --> 00:01:30.409 these extra bits of code that we no longer need so were gonna leave the first part which 21 00:01:30.409 --> 00:01:31.289 is correct 22 00:01:31.289 --> 00:01:35.530 which is generating the random number and printing it out on the screen and what we're 23 00:01:35.530 --> 00:01:40.210 going to do now is instead of putting guess = int(input) input what we need to do is 24 00:01:40.210 --> 00:01:45.880 initialized that to 0 because if you recall in our discussions with a while loop we need to 25 00:01:45.880 --> 00:01:51.149 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:51.149 --> 00:01:53.450 does, so its going to be 27 00:01:53.450 --> 00:02:04.619 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:04.619 --> 00:02:10.170 range as you saw when I created in the previous video then the while would not 29 00:02:10.170 --> 00:02:14.090 be started for the first time so making it zero when its going to be OK 30 00:02:14.090 --> 00:02:19.180 and because the reason we know that it's not a valid number is of course on line 15 we 31 00:02:19.180 --> 00:02:23.150 are getting a random number which starts on the low range as one and in the highest ranges 32 00:02:23.150 --> 00:02:27.720 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:27.720 --> 00:02:39.920 number, so to fix this now we need to change that to while loop so we gonna 34 00:02:39.920 --> 00:02:49.110 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:49.110 --> 00:02:53.150 will prompt for input which is correct and then we still need to put some tests in here 36 00:02:53.150 --> 00:03:03.840 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:03.840 --> 00:03:08.900 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:08.900 --> 00:03:13.250 a number that is lower than the answers, so its gonna say please guess higher, otherwise have typed in a 39 00:03:13.250 --> 00:03:18.940 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:18.940 --> 00:03:22.090 this next line actually out because we don't wanna input the 41 00:03:22.090 --> 00:03:30.430 code again what we really want to do is wanna put an else there, and I should 42 00:03:30.430 --> 00:03:34.569 have change this code here to be an elif 43 00:03:34.569 --> 00:03:46.849 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:46.849 --> 00:03:51.549 that guess is not equal to answer as soon as guess equals the loop is effectively going to end 45 00:03:51.549 --> 00:03:57.680 and so what happens is we do is guess = inputs so we are getting the integer from the keyboard 46 00:03:57.680 --> 00:04:03.980 if the typed number is less than ours were gonna print a message please guess higher, else if guess is 47 00:04:03.980 --> 00:04:09.620 greater than answer, then we print the message please guess lower than else if it 48 00:04:09.620 --> 00:04:15.019 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:15.019 --> 00:04:18.430 code gets down to hear that's not true it's gonna look back again until guess 50 00:04:18.430 --> 00:04:19.759 as equal to answer 51 00:04:19.759 --> 00:04:22.830 essentially as long as the guess doesn't equal to answer the loop’s continues to 52 00:04:22.830 --> 00:04:25.820 request a new guess and checking the answer and you can see now that the if 53 00:04:25.820 --> 00:04:28.680 statements are obviously a lot easier because of taking something out we just 54 00:04:28.680 --> 00:04:34.530 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:34.530 --> 00:04:38.320 that and make sure that it does work, please guess a number between 1 and 10 56 00:04:38.320 --> 00:04:45.849 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:45.849 --> 00:04:51.270 program so I need to close that, we are running the while program we created in 58 00:04:51.270 --> 00:04:55.590 the previous video, I need to right click and run this first, click challenge 59 00:04:55.590 --> 00:05:04.960 will try again, 5 again please guess higher, try 7 days, please guess higher will try 8 well 60 00:05:04.960 --> 00:05:18.710 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:18.710 --> 00:05:32.020 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:32.020 --> 00:05:36.870 optional challenge and of course the optional extra of this challenge is for 0 to 63 00:05:36.870 --> 00:05:41.909 break out of the loop and it's quite simple to do after the guess where we are 64 00:05:41.909 --> 00:05:46.120 entering the number or getting the number from the keyboard we put 65 00:05:46.120 --> 00:05:46.910 66 00:05:46.910 --> 00:05:58.870 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 67 00:05:58.870 --> 00:06:03.810 more interesting so if we close this down let's make the highest number a 1000 now 68 00:06:03.810 --> 00:06:07.770 what you should be able to do, is you have to guess the correct number within 10 69 00:06:07.770 --> 00:06:11.660 guesses, if looked at what I was doing initially probably got an idea what I 70 00:06:11.660 --> 00:06:15.870 was doing especially from the previous video and little bit in this 71 00:06:15.870 --> 00:06:20.440 video but there is a way of doing this so you can within 10 guesses no matter what the 72 00:06:20.440 --> 00:06:25.050 number is the, whatever the number ranges you should be able to determine that within 10 guesses 73 00:06:25.050 --> 00:06:28.940 So I’m going to leave that Im not going to show you how to do that, see if you can figure 74 00:06:28.940 --> 00:06:33.440 that out but you will find out how and why later in the course so that's it 75 00:06:33.440 --> 00:06:37.050 that's this video finished now and will see you in the next one