1
1

00:00:00,920  -->  00:00:02,460
<v Instructor>Hi guys and welcome back.</v>
2

2

00:00:02,460  -->  00:00:04,750
In this video we're going to look at some If statements
3

3

00:00:04,750  -->  00:00:07,490
with the in keyboard and also a few more examples
4

4

00:00:07,490  -->  00:00:09,600
so you get a bit more experience.
5

5

00:00:09,600  -->  00:00:13,130
Here we've got our movies watched which is a set of strings
6

6

00:00:13,130  -->  00:00:15,780
with the movie titles and then we're asking the user
7

7

00:00:15,780  -->  00:00:17,653
for a movie they've watched recently.
8

8

00:00:19,130  -->  00:00:23,510
We can then say user movie in movies watched
9

9

00:00:24,400  -->  00:00:27,140
and we can put that in an If statement.
10

10

00:00:27,140  -->  00:00:29,970
So again when Python reaches line four it's going
11

11

00:00:29,970  -->  00:00:33,010
to evaluate this Boolean and it's gonna say
12

12

00:00:33,010  -->  00:00:36,710
is the thing the user entered one of the elements
13

13

00:00:36,710  -->  00:00:38,923
of this collection, this set?
14

14

00:00:39,780  -->  00:00:42,960
And if it's true then it's going to evaluate this code here
15

15

00:00:42,960  -->  00:00:46,260
and remember we need indentation in front of this
16

16

00:00:46,260  -->  00:00:48,160
so that Python can tell what code
17

17

00:00:48,160  -->  00:00:50,550
is inside the If statement.
18

18

00:00:50,550  -->  00:00:52,610
So we're gonna print something like
19

19

00:00:52,610  -->  00:00:55,563
I've watched user movie too.
20

20

00:00:57,630  -->  00:01:01,523
Otherwise we can print I haven't watched that yet.
21

21

00:01:03,370  -->  00:01:07,500
If we run this then you can enter something like Green Book
22

22

00:01:07,500  -->  00:01:10,483
and it'll tell you that I've watched Green Book too.
23

23

00:01:11,640  -->  00:01:13,770
Of course if you enter something else
24

24

00:01:14,970  -->  00:01:18,120
then it'll tell you that I haven't watched that yet.
25

25

00:01:18,120  -->  00:01:20,400
So this is how you can use the in keyword
26

26

00:01:20,400  -->  00:01:24,800
in a If statement and it just worked in exactly the same way
27

27

00:01:24,800  -->  00:01:27,380
as it did in the last video when we were printing it out
28

28

00:01:27,380  -->  00:01:30,220
but now you can use it there in order to make decisions
29

29

00:01:30,220  -->  00:01:32,973
and potentially do different things in your programme.
30

30

00:01:33,830  -->  00:01:36,680
Let's work on a slightly different example.
31

31

00:01:36,680  -->  00:01:40,360
We're going to create now a magic number App.
32

32

00:01:40,360  -->  00:01:42,770
So in this application we're going to decide
33

33

00:01:42,770  -->  00:01:45,850
on a magic number, a hidden number from the user.
34

34

00:01:45,850  -->  00:01:48,270
We're going to ask the user to enter a number
35

35

00:01:48,270  -->  00:01:51,750
and then we are going to tell the user
36

36

00:01:51,750  -->  00:01:54,013
whether they got the number right or not.
37

37

00:01:54,990  -->  00:01:57,560
But before we do that we're going to ask the user
38

38

00:01:57,560  -->  00:02:00,070
whether they want to play the game or not.
39

39

00:02:00,070  -->  00:02:02,610
So we'll start by creating our magic number
40

40

00:02:02,610  -->  00:02:05,330
we'll say number equal seven.
41

41

00:02:05,330  -->  00:02:07,890
Then we're gonna ask the user whether they want to play.
42

42

00:02:07,890  -->  00:02:12,140
So we'll say user input equal input enter Y
43

43

00:02:13,070  -->  00:02:14,733
if you would like play.
44

44

00:02:16,910  -->  00:02:20,933
Now we're gonna say if user input equal equal Y,
45

45

00:02:22,120  -->  00:02:25,570
then we will ask them for a number.
46

46

00:02:25,570  -->  00:02:29,950
So we can do something like user number int of input,
47

47

00:02:29,950  -->  00:02:31,950
Guess our Number let's say.
48

48

00:02:31,950  -->  00:02:34,510
So we will ask them to guess our number,
49

49

00:02:34,510  -->  00:02:36,970
we will get the input that they type
50

50

00:02:36,970  -->  00:02:39,140
and then we will turn that into an integer
51

51

00:02:40,120  -->  00:02:42,310
and finally we can use another If statement here
52

52

00:02:42,310  -->  00:02:45,350
and say user number equal equal number
53

53

00:02:45,350  -->  00:02:47,650
then we will print that you guessed correctly.
54

54

00:02:50,530  -->  00:02:53,593
Otherwise we will print sorry it's wrong.
55

55

00:02:55,650  -->  00:02:58,180
So we're not using the in keyword here at all
56

56

00:02:58,180  -->  00:02:59,890
but there are a few things that I wanted to tell you
57

57

00:02:59,890  -->  00:03:01,940
about before we do that.
58

58

00:03:01,940  -->  00:03:05,530
The first one is that all of these lines are inside
59

59

00:03:05,530  -->  00:03:06,363
this If statement.
60

60

00:03:06,363  -->  00:03:10,690
So they will only ever potentially run if this is true.
61

61

00:03:10,690  -->  00:03:13,340
So if the user enters lowercase Y
62

62

00:03:13,340  -->  00:03:16,770
then we will start running this line number five,
63

63

00:03:16,770  -->  00:03:19,640
then we will move on to six and so on.
64

64

00:03:19,640  -->  00:03:22,970
Number six here is another If statement,
65

65

00:03:22,970  -->  00:03:26,620
so if this is true then we will run this line
66

66

00:03:26,620  -->  00:03:29,480
and now you can see that this line has two sets
67

67

00:03:29,480  -->  00:03:33,910
of indentation because it is both inside this If statement
68

68

00:03:33,910  -->  00:03:38,910
and this one so therefore we have four eight spaces in total
69

69

00:03:39,360  -->  00:03:41,710
in front of it so the Python can tell that
70

70

00:03:41,710  -->  00:03:44,173
this one belongs inside this If statement.
71

71

00:03:45,220  -->  00:03:47,300
Then we've got an else paired
72

72

00:03:47,300  -->  00:03:50,100
with this if statement in line six,
73

73

00:03:50,100  -->  00:03:51,900
which means that if the user number
74

74

00:03:51,900  -->  00:03:55,560
was not exactly correct given our magic number.
75

75

00:03:55,560  -->  00:03:57,730
We're going to print sorry it's wrong
76

76

00:03:57,730  -->  00:04:00,030
and again notice the indentation in this line.
77

77

00:04:00,960  -->  00:04:03,440
We can run this code and then we'll ask the user
78

78

00:04:03,440  -->  00:04:05,310
to enter why if they would like to play,
79

79

00:04:05,310  -->  00:04:07,870
so we are gonna enter Y and then we can guess the number,
80

80

00:04:07,870  -->  00:04:09,750
let's say seven and you can see
81

81

00:04:09,750  -->  00:04:12,010
that we guessed it correctly.
82

82

00:04:12,010  -->  00:04:14,450
Now let's start using the in keyword
83

83

00:04:14,450  -->  00:04:16,770
because the first thing that you may see users do
84

84

00:04:16,770  -->  00:04:18,780
is that they may think that because it is a computer
85

85

00:04:18,780  -->  00:04:21,520
or something they have to enter uppercase Y.
86

86

00:04:21,520  -->  00:04:24,440
It is quite common in computing to ask the user
87

87

00:04:24,440  -->  00:04:27,490
for uppercase letters so they might do that
88

88

00:04:27,490  -->  00:04:29,680
and here you can see that we don't support
89

89

00:04:29,680  -->  00:04:31,503
this uppercase variant.
90

90

00:04:32,550  -->  00:04:34,690
So there are two things you can do.
91

91

00:04:34,690  -->  00:04:36,120
The first one that we've already looked at
92

92

00:04:36,120  -->  00:04:38,150
is you can turn this into lowercase
93

93

00:04:38,150  -->  00:04:40,900
and that is actually the better choice of the two.
94

94

00:04:40,900  -->  00:04:43,650
So I would do this I would turn the users input
95

95

00:04:43,650  -->  00:04:44,920
into lowercase and then
96

96

00:04:44,920  -->  00:04:47,470
I would compare the lowercase user input
97

97

00:04:47,470  -->  00:04:50,970
with the lowercase character that I wanna check.
98

98

00:04:50,970  -->  00:04:52,140
But in some situations
99

99

00:04:52,140  -->  00:04:55,120
you may want different options potentially
100

100

00:04:55,120  -->  00:04:57,030
or you may not wanna do that exactly.
101

101

00:04:57,030  -->  00:04:59,250
So there is something you can do in this If statement
102

102

00:04:59,250  -->  00:05:03,740
which is to replace the Equal Equal for In
103

103

00:05:03,740  -->  00:05:08,740
and then you can have here a tuple of the two letters
104

104

00:05:09,440  -->  00:05:12,280
that you're willing to accept as your input.
105

105

00:05:12,280  -->  00:05:14,210
So what we do here is we create a tuple
106

106

00:05:14,210  -->  00:05:18,050
with two strings, lowercase Y and uppercase Y
107

107

00:05:18,050  -->  00:05:20,930
and then we're checking whether the users input
108

108

00:05:20,930  -->  00:05:22,840
is one of those two.
109

109

00:05:22,840  -->  00:05:25,503
And if it is then we will proceed.
110

110

00:05:26,550  -->  00:05:28,530
So this is something you could do if you were interested
111

111

00:05:28,530  -->  00:05:31,400
I'm just showing you that there but I would actually prefer
112

112

00:05:31,400  -->  00:05:33,763
to turn the users input into the lowercase.
113

113

00:05:34,870  -->  00:05:38,820
The second place where we could use the in keyword
114

114

00:05:38,820  -->  00:05:42,870
is if we wanted to give the user a hint regarding how close
115

115

00:05:42,870  -->  00:05:45,740
or how far they were from our magic number.
116

116

00:05:45,740  -->  00:05:47,410
So let's say that we wanna tell the user
117

117

00:05:47,410  -->  00:05:51,793
that they are off by one, if they enter six or eight.
118

118

00:05:53,090  -->  00:05:54,210
Here in the If statement
119

119

00:05:54,210  -->  00:05:57,060
after user number equal equal number,
120

120

00:05:57,060  -->  00:06:02,060
we're going to say elif number minus user number,
121

121

00:06:02,290  -->  00:06:07,290
in one and minus one we're gonna print you were off by one.
122

122

00:06:09,450  -->  00:06:12,210
So what this is doing is once again it's defining a tuple
123

123

00:06:12,210  -->  00:06:14,740
of two numbers that we're willing to accept,
124

124

00:06:14,740  -->  00:06:16,880
one and minus one.
125

125

00:06:16,880  -->  00:06:20,240
Then we'll go into subtract from our number
126

126

00:06:20,240  -->  00:06:21,970
which is seven the user number.
127

127

00:06:21,970  -->  00:06:24,280
let's say six as an example.
128

128

00:06:24,280  -->  00:06:26,880
Seven minus six is going to give us one
129

129

00:06:26,880  -->  00:06:28,880
and then it's going to check whether one
130

130

00:06:28,880  -->  00:06:32,780
is in this tuple which has one and minus one.
131

131

00:06:32,780  -->  00:06:35,540
If it is then we're gonna print that you are off by one.
132

132

00:06:35,540  -->  00:06:38,950
If they enter eight then we will do seven minus eight
133

133

00:06:38,950  -->  00:06:41,080
which is minus one and minus one
134

134

00:06:41,080  -->  00:06:42,910
will also be in this tuple.
135

135

00:06:42,910  -->  00:06:45,143
So we will also print you were off by one.
136

136

00:06:46,450  -->  00:06:48,430
This is something you could do and potentially
137

137

00:06:48,430  -->  00:06:52,050
this can be really handy if you want a longer set of values
138

138

00:06:52,050  -->  00:06:55,620
but for this specific situation there's also a better ways
139

139

00:06:55,620  -->  00:06:57,890
of doing it without the in keyword
140

140

00:06:57,890  -->  00:07:00,490
which is by using absolute values.
141

141

00:07:00,490  -->  00:07:02,393
So you can do ABS,
142

142

00:07:03,980  -->  00:07:06,070
of number minus user number
143

143

00:07:06,070  -->  00:07:08,700
and what ABS does is it gives you the absolute value
144

144

00:07:08,700  -->  00:07:09,533
of this argument.
145

145

00:07:09,533  -->  00:07:13,633
So if you pass it the value one ABS of one
146

146

00:07:13,633  -->  00:07:17,260
will give you one but if you pass it minus one,
147

147

00:07:17,260  -->  00:07:19,900
ABS of minus one will give you one.
148

148

00:07:19,900  -->  00:07:22,200
It will turn it back into positive.
149

149

00:07:22,200  -->  00:07:25,280
So now number minus user number will always give you,
150

150

00:07:25,280  -->  00:07:29,440
how far away the user was from our magic number
151

151

00:07:29,440  -->  00:07:31,130
in positive terms.
152

152

00:07:31,130  -->  00:07:33,793
So you can go back to say an equal equal one.
153

153

00:07:34,820  -->  00:07:38,410
Because this number here will always be one, two, three
154

154

00:07:38,410  -->  00:07:39,900
and so on.
155

155

00:07:39,900  -->  00:07:43,357
So if they were off by one then we can just print that.
156

156

00:07:43,357  -->  00:07:45,420
And I think this is a slightly better approach
157

157

00:07:45,420  -->  00:07:48,340
than using the in keyword but again the in keyword can
158

158

00:07:48,340  -->  00:07:50,500
be useful in a wide number of scenarios
159

159

00:07:50,500  -->  00:07:52,423
where this may not be enough.
160

160

00:07:53,840  -->  00:07:57,690
If we run this again you'll see that we can type Y
161

161

00:07:57,690  -->  00:08:00,830
to play then we can guess the number let's say eight
162

162

00:08:00,830  -->  00:08:03,640
and then it says that you were off by one.
163

163

00:08:03,640  -->  00:08:06,370
Feel free to play around with this code and change it
164

164

00:08:06,370  -->  00:08:08,670
and do more things with it if you'd like.
165

165

00:08:08,670  -->  00:08:11,270
This is a great exercise and will be good practise for you
166

166

00:08:11,270  -->  00:08:14,120
if you're starting to learn to code.
167

167

00:08:14,120  -->  00:08:15,510
Thanks for joining me in this video
168

168

00:08:15,510  -->  00:08:18,303
I hope it's been useful and I'll see you in the next one.
