1
00:00:04,680 --> 00:00:10,000
In an earlier video on if statements,
we created a very simple guessing game.

2
00:00:10,000 --> 00:00:13,760
The player had to guess a number
between 1 and 10. We want to open up

3
00:00:13,760 --> 00:00:22,980
that guessinggame.py file and work
on that in this video. Now what I'm

4
00:00:22,980 --> 00:00:26,780
going to do is delete the commented out
code at the end, because we no longer

5
00:00:26,789 --> 00:00:35,370
need that. Alright so there's our code at
the moment. Now as it stands there's a

6
00:00:35,370 --> 00:00:39,420
number of problems with this game.
One problem is that it only allows two

7
00:00:39,420 --> 00:00:43,680
guesses. If we wanted to allow more than
two guesses, we'd have to nest more and

8
00:00:43,680 --> 00:00:48,420
more if tests every time the player
guessed incorrectly. The other problem is

9
00:00:48,420 --> 00:00:52,280
that five is always the correct number,
which makes the game a bit boring,

10
00:00:52,280 --> 00:00:57,810
and not that it was terribly engaging to
begin with. Now the first problem can be

11
00:00:57,810 --> 00:01:01,710
solved using a while loop, which is what
you'll be doing in the challenge coming

12
00:01:01,710 --> 00:01:06,720
up next. Before we do that, though, let's
digress slightly and address the second

13
00:01:06,720 --> 00:01:11,250
problem, so that we can produce a more
interesting game. To fix the problem of

14
00:01:11,250 --> 00:01:15,480
the correct number always being five, we
need to get the computer to generate a

15
00:01:15,480 --> 00:01:20,400
random number in the chosen range - that's
one to ten in the example - but we can

16
00:01:20,400 --> 00:01:25,860
extend that. Python has a lot of features
built in such as the ability to do

17
00:01:25,860 --> 00:01:31,470
arithmetic, lists, ranges and tuples and
more. There's enough built in, in fact, for

18
00:01:31,470 --> 00:01:36,420
us to be able to write a random number
generator if we wanted to do that.

19
00:01:36,420 --> 00:01:40,340
The good news is though, however, one has been
written for us and comes included

20
00:01:40,340 --> 00:01:46,580
with Python in a module called random.
We'll be looking at creating modules and

21
00:01:46,580 --> 00:01:50,240
packages much later in the course, but
for now what we're going to do is use

22
00:01:50,240 --> 00:01:55,100
the one, the random generator, that comes
with Python. Because it's in an external

23
00:01:55,100 --> 00:01:59,390
module, we need to import it into our
program to use it, using an import

24
00:01:59,390 --> 00:02:03,290
statement. When you want to use objects
from the Python standard library you

25
00:02:03,290 --> 00:02:08,090
import them, usually at the start of your
program. Importing a module allows you to

26
00:02:08,090 --> 00:02:14,480
use all the objects from that module in
your own code. So we want to use the rand

27
00:02:14,480 --> 00:02:19,280
int function from the random module, so
we can start by importing random. We're

28
00:02:19,280 --> 00:02:26,990
going to do that right up on line one, by
typing import random. Now that we've

29
00:02:26,990 --> 00:02:31,360
imported the module we can use its rand
int function on line three. So let's

30
00:02:31,360 --> 00:02:42,400
change the five, the literal value
five to random dot randint parentheses

31
00:02:42,400 --> 00:02:49,300
1 comma 10. I've used random.randint
on line 3 because the randint function

32
00:02:49,300 --> 00:02:53,180
is in the random module. We imported
random on line 1 at the start of the

33
00:02:53,180 --> 00:02:57,440
program and we can now use the functions
it contains by using what's called dot

34
00:02:57,440 --> 00:03:02,480
notation. We separate the module name
from the function name with a period or

35
00:03:02,480 --> 00:03:07,489
dot, which is why it's called dot
notation. We're using dot notation to

36
00:03:07,489 --> 00:03:11,980
tell Python that the randint function
is found in the random module.

37
00:03:11,980 --> 00:03:16,840
The randint function produces a random
number within the range we specify.

38
00:03:16,850 --> 00:03:20,480
We've passed the values 1 to 10 to the
function, which means we'll get a random

39
00:03:20,480 --> 00:03:26,620
number from 1 to 10 inclusive. So we've
used the value 10 in two places in our code.

40
00:03:26,620 --> 00:03:29,960
If we wanted to increase the range
of numbers that the player has to guess,

41
00:03:29,960 --> 00:03:34,100
we'd have to make a change in both of
these places. That's a good way to

42
00:03:34,100 --> 00:03:38,900
introduce bugs in your code, so if we
changed the value on line 3 but forgot to

43
00:03:38,900 --> 00:03:43,370
change the message on line 5, the program
wouldn't behave as the player would

44
00:03:43,370 --> 00:03:48,080
expect it to. We can make the code more
robust by using a variable to store the

45
00:03:48,080 --> 00:03:52,700
upper value, so let's have a go at doing
that. So on line 3 I'm going to type

46
00:03:52,700 --> 00:03:58,800
highest is equal to 10.
Then on line four I'm going to change the

47
00:03:58,800 --> 00:04:05,040
literal value 10 to highest, like so, and
then what we're going to do is change

48
00:04:05,040 --> 00:04:09,960
our message here, delete 10 and put
left and right curly braces instead,

49
00:04:09,960 --> 00:04:16,820
and after the double quote put .format and
in parentheses, highest. Now we can increase

50
00:04:16,829 --> 00:04:21,180
the range if we wanted to by making a
single change on line three and a change

51
00:04:21,180 --> 00:04:25,920
would be reflected in both places.
Alright so let's run the program and see

52
00:04:25,920 --> 00:04:30,990
if we can guess the number. So I'm going
to guess five the first time, and if

53
00:04:30,990 --> 00:04:34,050
that's too low I'm going to guess eight,
otherwise I'm going to guess three,

54
00:04:34,050 --> 00:04:38,580
unless five happens to be correct of
course. So let's try five and I got it first

55
00:04:38,580 --> 00:04:43,500
time, amazing! It is actually quite hard
to test this program properly, unless

56
00:04:43,500 --> 00:04:46,770
we're really lucky. We can't test that it
prints the right message when we guess

57
00:04:46,770 --> 00:04:52,420
correctly, but of course I got it correctly
the first time, so let's try running it again.

58
00:04:52,420 --> 00:04:56,160
I'll try five again, Please get higher.

59
00:04:56,160 --> 00:05:00,320
This time I'm going to guess 8, as I
mentioned I would do, and it was too low,

60
00:05:00,320 --> 00:05:04,169
and well done I guessed it. Well I'm having
an amazing amount of lucky here because this

61
00:05:04,169 --> 00:05:08,080
is entirely random.
We'll try one more time.

62
00:05:08,080 --> 00:05:14,900
We'll try five again and that's just,
again, amazing. Three times out of three.

63
00:05:14,900 --> 00:05:19,320
Please guess higher. We'll try eight
again. This time finally, we've got it wrong.

64
00:05:19,320 --> 00:05:22,290
Sorry you have not guessed
correctly and I'll try one more time just to

65
00:05:22,290 --> 00:05:26,940
see if we can get the opposite to happen,
five again, Please guess lower this time.

66
00:05:26,940 --> 00:05:31,290
I'm going to try three, Well done, you
guessed it. Again I'm having an amazing run

67
00:05:31,290 --> 00:05:37,650
of luck here. One more time - I can't believe
I've got this right so many times but we'll try

68
00:05:37,650 --> 00:05:41,940
one more time, Please guess higher.
I'll try eight and we'll leave it at

69
00:05:41,940 --> 00:05:46,020
that. So we didn't get to see us not
guessing it correctly by guessing a lower

70
00:05:46,020 --> 00:05:48,720
one, but that's fine.
That really alludes to the fact that it

71
00:05:48,720 --> 00:05:52,229
is quite hard to test the program
properly, because even if it does print

72
00:05:52,229 --> 00:05:55,950
the message we can't tell if it should
because we've got no idea of what the

73
00:05:55,950 --> 00:05:59,310
correct answer is. So to get around that
while testing, what we can do is print

74
00:05:59,310 --> 00:06:03,300
the value of answer out. So for testing
purposes, it just makes it easier for us

75
00:06:03,300 --> 00:06:07,380
to know rather than just guessing, so
what I'm going to do is add the code

76
00:06:07,380 --> 00:06:13,460
on line five. I'm going to do print answer,
and that's gonna be the answer

77
00:06:13,460 --> 00:06:17,130
from the, so that's basically the number
that's been randomly generated. I'm also

78
00:06:17,130 --> 00:06:23,460
going to put a comment here, hash TODO
remove after testing. Obviously you wouldn't

79
00:06:23,460 --> 00:06:27,780
want to leave this in, in a real
game, because of course what would happen

80
00:06:27,780 --> 00:06:31,080
would be the player would know the
number to guess each time. Now you might be

81
00:06:31,080 --> 00:06:35,250
wondering what happened with that TODO.
Two things; firstly, it changed to blue

82
00:06:35,250 --> 00:06:40,470
when I typed TODO, but secondly, IntelliJ
treats comments that start with TODO in

83
00:06:40,470 --> 00:06:45,140
a special way. It highlights them in blue
with my color scheme and also keeps

84
00:06:45,140 --> 00:06:48,780
track of them. Down at the bottom of the screen
we've got a TODO tab. I'm gonna come

85
00:06:48,780 --> 00:06:54,330
down here and click on that. If we open that
up we can see that it'll give us a list

86
00:06:54,330 --> 00:06:58,200
of all the TODOs, in this case, just in
the one file we've got, and it actually

87
00:06:58,200 --> 00:07:01,710
tells us there that on line five, we've
got print answer and it shows us the

88
00:07:01,710 --> 00:07:06,000
TODO that I've actually entered. So you
can basically expand the sections and

89
00:07:06,000 --> 00:07:09,720
double click on a particular entry to go
straight to that line in your code, so

90
00:07:09,720 --> 00:07:13,800
for example, if I was down there, double
click it, my cursor moves back up the

91
00:07:13,800 --> 00:07:17,190
line five - very useful if you happen to
be in another file at the time. So in

92
00:07:17,190 --> 00:07:20,550
general, TODOs are a great way to keep
track of things so that you don't forget

93
00:07:20,550 --> 00:07:25,320
about changes you intended to make. Note
that this is a feature of IntelliJ, the

94
00:07:25,320 --> 00:07:30,700
IDE. It's not a Python feature. Python
just treats that comment like any other.

95
00:07:30,700 --> 00:07:33,690
Alright, so at this point now, the program
should be easy to test, even though I had

96
00:07:33,690 --> 00:07:39,720
a good run of luck last time. I'll just
close down the TODO tab. Let's run it again,

97
00:07:39,720 --> 00:07:42,060
and what I can do is check what happens

98
00:07:42,060 --> 00:07:46,620
if I get the correct result first time,
and on my second guess. So the first time,

99
00:07:46,620 --> 00:07:51,000
we know that the value is one there.
So we enter 1, we got it first time so

100
00:07:51,000 --> 00:07:53,480
we know that that's working. We'll
try it again,

101
00:07:53,480 --> 00:07:56,640
this time I'll enter a number that I
know is too high,

102
00:07:56,640 --> 00:08:01,950
8, Please guess lower. Well I could
guess 5, guessing the number correctly

103
00:08:01,950 --> 00:08:06,450
on the second time, Well done you've guessed
it. And obviously, now that you know the

104
00:08:06,450 --> 00:08:09,900
number, you could go through all the different
scenarios of testing, to make sure you

105
00:08:09,900 --> 00:08:14,070
fully test all program flow conditions.
So at this point, you'll probably find

106
00:08:14,070 --> 00:08:17,550
the program a bit frustrating. With ten
numbers to choose from we'd probably

107
00:08:17,550 --> 00:08:21,150
need a minimum of four guesses, to be
sure of getting the number if you didn't

108
00:08:21,150 --> 00:08:26,760
know the answer in advance, and that
sounds like a good challenge to try.

109
00:08:26,760 --> 00:08:28,310
So the challenge is to modify the

110
00:08:28,310 --> 00:08:33,349
program to use a while loop, to allow the
player to keep guessing. The program

111
00:08:33,349 --> 00:08:36,770
should let the player know whether to
guess higher or lower, and should print a

112
00:08:36,770 --> 00:08:40,219
message when the guess is correct.
Now it already does that, but I suggest

113
00:08:40,219 --> 00:08:43,250
you don't worry about printing a
different message, if they get the

114
00:08:43,250 --> 00:08:46,700
correct answer on their first guess.
We'll look at counting the number of

115
00:08:46,700 --> 00:08:50,840
guesses later. A correct guess will
terminate the program. Now as an

116
00:08:50,840 --> 00:08:56,210
optional extra, allow the player to quit
by entering 0 for their guess. So these

117
00:08:56,210 --> 00:08:59,460
challenges are intended to give you a
chance to practice what you've learned.

118
00:08:59,460 --> 00:09:03,520
Don't think of them as tests. There's no
pass or fail as such. If you have a go but

119
00:09:03,530 --> 00:09:07,070
don't manage to solve the entire
challenge, or even part of it, that's fine.

120
00:09:07,070 --> 00:09:10,880
Generally, most people, even if they don't
successfully complete it, will learn

121
00:09:10,880 --> 00:09:14,060
something from the experience, so it's
well worth giving it a go. The bottom

122
00:09:14,060 --> 00:09:17,690
line is, you'll learn a lot more by
trying before you watch my solution

123
00:09:17,690 --> 00:09:20,720
coming up in the next video, than you
would if I just presented you with the

124
00:09:20,720 --> 00:09:24,140
working code. So have a go and see how
you go with it, and we'll go over that

125
00:09:24,140 --> 00:09:28,000
solution together in the next video.

