WEBVTT

0
00:00.410 --> 00:04.550
All right, so I really hope you've given this challenge a good go,

1
00:04.550 --> 00:11.900
and you've spent at least an hour toiling over it and struggling, and you should be wiping sweat off

2
00:11.900 --> 00:15.650
your brows before you come and look at the solution.

3
00:15.710 --> 00:22.280
The struggle is so important and I can't stress it enough, but in this lesson I want to run through

4
00:22.310 --> 00:24.290
how I would tackle this problem.

5
00:24.380 --> 00:27.380
Now, it doesn't mean that you have to do the same thing,

6
00:27.380 --> 00:28.730
there isn't one solution.

7
00:28.730 --> 00:32.540
There's probably thousands of different solutions for a project like this.

8
00:32.540 --> 00:37.490
And as long as your program runs exactly the same way, then you've succeeded.

9
00:37.490 --> 00:40.970
It doesn't matter at this stage how you've done it necessarily,

10
00:40.970 --> 00:46.220
as long as you managed to organize your thoughts, break down the problem, and write the solution code

11
00:46.220 --> 00:49.700
and get it to run, then you are victorious.

12
00:49.820 --> 00:57.650
The real challenge in today's project was for you to break down a big problem into smaller, solvable

13
00:57.650 --> 00:58.280
chunks.

14
00:58.310 --> 01:01.170
It isn't even so much the code that matters,

15
01:01.200 --> 01:05.940
it's this skill that you really need to develop to become a really good programmer.

16
01:05.970 --> 01:12.240
And so far, I've been breaking down the problem for you, showing you the steps that would take you

17
01:12.240 --> 01:13.410
to the solution,

18
01:13.410 --> 01:18.390
but now you're really wearing the big programmer pants and you're doing it all by yourself.

19
01:18.510 --> 01:22.980
So let's think about this problem and how we would break it down.

20
01:23.010 --> 01:25.140
There's some easy problems here, right?

21
01:25.170 --> 01:26.970
We need the art to display...

22
01:26.970 --> 01:30.360
So let's add a comment for each problem we identify.

23
01:30.390 --> 01:35.760
We've got the ASCII art showing up here, and we've got the Vs showing up here.

24
01:35.940 --> 01:43.140
Now the next one is we've got these randomly generated accounts, from the data.

25
01:43.140 --> 01:46.290
So we've got Camila Cabello and Emma Watson.

26
01:46.440 --> 01:49.170
How do we get these random accounts to show up,

27
01:49.170 --> 01:50.490
that's another problem.

28
01:50.520 --> 01:51.120
Right?

29
01:51.120 --> 01:58.230
So we know that we've got all of this game data here in the format of a dictionary that's nested inside

30
01:58.230 --> 01:58.920
a list,

31
01:58.920 --> 02:04.230
and we need to figure out a way of pulling out a random account from this list.

32
02:04.230 --> 02:07.140
So that's another problem I've identified.

33
02:07.170 --> 02:13.860
Now, the next thing you'll notice is that the data contains many key/value pairs, the name of the account,

34
02:13.860 --> 02:20.730
the description, et cetera, but when we display it, it's all nicely formatted, the name, and then what they

35
02:20.730 --> 02:22.740
do, and then where they're from.

36
02:22.740 --> 02:26.340
We'll have to add that as probably another problem to solve.

37
02:26.340 --> 02:33.390
How do we format the account data into a printable format?

38
02:33.390 --> 02:33.900
Okay,

39
02:33.930 --> 02:37.320
so let's say we solve that and we've got A and B.

40
02:37.350 --> 02:39.720
Now we have to ask the user for a guess.

41
02:39.750 --> 02:41.490
Guess who has more followers.

42
02:41.490 --> 02:43.950
So that's another problem to solve.

43
02:44.370 --> 02:52.860
And when they do type an answer, let's say we think maybe A has more followers, then it's going to

44
02:52.860 --> 02:57.210
have to check that answer and see if it's correct or if it's wrong.

45
02:57.240 --> 02:59.580
So that's going to be a big problem,

46
02:59.580 --> 03:05.340
"Check if user is correct," because that's going to require a couple of things, right?

47
03:05.370 --> 03:11.820
We're going to probably have to break down this problem into smaller problems if we want to solve it.

48
03:11.850 --> 03:17.490
So for example, we probably need to figure out the follower count of each account,

49
03:17.490 --> 03:25.830
and then we'll probably need to use an if statement to check if user is correct.

50
03:26.100 --> 03:31.770
Now, if we get it right or if we get it wrong, the game is always going to give us some feedback.

51
03:31.770 --> 03:35.580
"Sorry, that's wrong..." or "You're right!"

52
03:36.060 --> 03:38.370
So let's add to our problem list.

53
03:38.940 --> 03:43.950
Now, in addition to giving them feedback, whether if they got it right or whether if they got it wrong,

54
03:43.950 --> 03:46.830
it also has a way of tracking score, right?

55
03:46.860 --> 03:50.490
Every time they get an answer right, the score goes up by one.

56
03:50.580 --> 03:56.100
In addition to all of that, the game repeats itself when the user gets it right.

57
03:56.130 --> 04:03.460
So let's say in this case, I guess a Dwayne Johnson, then you see how the game repeats itself, right?

58
04:03.490 --> 04:10.450
It goes back and it takes Dwayne Johnson, puts it in position A and generates a new account in position

59
04:10.480 --> 04:12.700
B for me to go again.

60
04:12.700 --> 04:15.520
So I get another round because I got it right.

61
04:15.640 --> 04:18.550
So we'll need to address that in our code as well.

62
04:18.580 --> 04:24.130
Have some sort of way of, "Make the game repeatable."

63
04:25.420 --> 04:33.340
And it's not just repeatable actually, because we also have to move the previous B up to the A position

64
04:33.340 --> 04:35.800
if they do get it right for the next round.

65
04:35.800 --> 04:41.200
So I think Khloe Kardashian maybe has less,

66
04:42.340 --> 04:47.410
oh. So let's say I guess Dwayne Johnson again in this case.

67
04:47.410 --> 04:54.340
Now if I get it right, Taylor Swift, who used to be at position B, now gets moved to position A ready

68
04:54.340 --> 04:55.480
for the next round.

69
04:55.480 --> 05:02.660
So this is another problem we have to tackle. So not only does the game have to be repeatable, we also

70
05:02.660 --> 05:13.160
have to have some sort of way of making the accounts at position B become the next account at position

71
05:13.190 --> 05:13.760
A.

72
05:13.790 --> 05:15.830
That's it, I think, for now.

73
05:15.860 --> 05:23.150
I've broken down this large problem, which is build this game into a bunch of smaller problems,

74
05:23.150 --> 05:27.470
and as we code along, we're probably going to break them down even further.

75
05:27.500 --> 05:31.400
This really was the key skill that I wanted to test you on,

76
05:31.430 --> 05:37.130
being able to take a big project, turn it into smaller problems, and then tackle them one by one.

77
05:37.130 --> 05:42.020
So if you managed to get this far, and you've managed to identify all the problems that you need to solve,

78
05:42.050 --> 05:44.840
you're ready to consider yourself successful.

79
05:44.870 --> 05:50.360
Now the next stage, we're going to try and tackle each of these one by one and be able to create the

80
05:50.360 --> 05:51.830
final version of the game.

81
05:53.060 --> 05:54.380
Let's get started.

82
05:54.380 --> 05:59.750
Now the first thing is probably the easiest, so I'm going to start from there to display the art,

83
06:00.020 --> 06:02.120
which is going to be the logo to begin with.

84
06:02.120 --> 06:08.960
I'm going to have to import that art file, and I'm going to use the, 'from art import logo',

85
06:08.990 --> 06:13.040
this way I don't have to write art.logo every time I want to use it.

86
06:13.340 --> 06:16.490
Now I can simply just write, print(logo).

87
06:16.610 --> 06:19.010
So that's the first one tackled.

88
06:19.040 --> 06:20.150
That was pretty easy.

89
06:20.180 --> 06:22.340
Some of them are going to be a lot harder than that.

90
06:22.520 --> 06:26.150
The next one is to generate a random account from the game_data.

91
06:26.150 --> 06:29.270
And remember the game_data is here in a separate file,

92
06:29.270 --> 06:33.350
and the data is held inside a variable called data.

93
06:33.980 --> 06:37.250
So let's go to the top and let's do the same thing,

94
06:37.250 --> 06:40.790
so, 'from game_data import data'.

95
06:40.820 --> 06:45.380
And now we can use that data to generate a random account.

96
06:46.250 --> 06:52.250
In order to generate a random item from that list we have to use the Random Module.

97
06:52.250 --> 06:55.700
So I'm going to import the random module.

98
06:56.150 --> 07:04.470
And I can use that random module to tap into the choice() function and pass in my data, which is a list.

99
07:04.470 --> 07:10.860
So this function is going to take this list and then find a random entry in there, which will give

100
07:10.860 --> 07:12.120
me a random account.

101
07:12.180 --> 07:22.080
Let's call this account may be, account_a and set that equal to the output from this random.choice()

102
07:22.080 --> 07:22.890
function.

103
07:22.920 --> 07:30.180
Now I'm probably going to need more than one random account, because I need to compare A against B.

104
07:30.720 --> 07:32.670
By calling random.choice() on data

105
07:32.700 --> 07:36.870
again, I make sure that I get a different random account.

106
07:37.350 --> 07:43.890
So at this point, we've got account_a and account_b, which hopefully are different from each other,

107
07:43.890 --> 07:47.790
but if you wanted to, you could in fact check and just make sure.

108
07:47.790 --> 07:55.380
So if account_a == account_b, then, in this case, you make sure that account_b is regenerated.

109
07:56.760 --> 08:00.090
Often as you're coding along tackling the problems,

110
08:00.090 --> 08:06.390
smaller problems arise and you start thinking about what could happen, and you start addressing that

111
08:06.390 --> 08:07.560
with code as well.

112
08:08.010 --> 08:14.340
So I think we're now done for this second problem, and we're ready to move on to the next one.

113
08:14.370 --> 08:18.510
Let's format the account data so that we can print it out in this format.

114
08:18.510 --> 08:25.710
So the account name, and then 'a', and then whatever it is that they do, and finally from and where they're

115
08:25.710 --> 08:26.340
from.

116
08:26.790 --> 08:32.700
So we know that the data of each of these accounts is basically a dictionary,

117
08:32.700 --> 08:36.690
and we know that we access dictionaries using the key/value pair,

118
08:36.690 --> 08:45.600
so we could, for example, access the account_name by tapping into the random account, let's say,

119
08:45.630 --> 08:46.320
account_a,

120
08:46.770 --> 08:49.500
and then we use that square bracket,

121
08:49.500 --> 08:54.330
and then we put in the key in here, which let me just check, is the name.

122
08:54.330 --> 08:56.280
So let's put that right here.

123
08:56.280 --> 09:03.310
And this will go into that dictionary, and pull out the value under the key name, and save it to this

124
09:03.310 --> 09:04.210
variable.

125
09:04.240 --> 09:08.230
And we can do the same thing for the other two pieces of data we need.

126
09:08.350 --> 09:13.150
And just make sure that you don't have any typos in your keys here,

127
09:13.150 --> 09:18.850
because remember they need to match these precisely in order for it to not error out.

128
09:20.050 --> 09:26.470
Now that we've got all of these pieces of data, we can now print the final version.

129
09:26.470 --> 09:30.970
So we're probably going to use an f-string to insert all of these pieces of information.

130
09:30.970 --> 09:37.120
And we'll probably start off with the account_name and then a comma,

131
09:37.150 --> 09:41.470
we can write 'a' and then put in the account_descr.

132
09:42.220 --> 09:46.630
And finally, put 'from' and the account_country.

133
09:48.700 --> 09:50.530
That's pretty much it.

134
09:50.530 --> 09:54.070
But notice how I've done this just for account_a,

135
09:54.070 --> 09:58.750
and I actually have to do it for both account_a, and account_b.

136
09:58.780 --> 10:05.200
So instead of repeating all of this code, it might be a lot easier if I actually pull this out into

137
10:05.200 --> 10:06.640
a separate function.

138
10:06.640 --> 10:08.590
So let's create that here.

139
10:08.920 --> 10:11.380
Let's create a function that does this.

140
10:11.440 --> 10:16.510
I'm going to turn this into a docstring to explain the function,

141
10:16.510 --> 10:19.420
and I'm going to call the function format_data().

142
10:19.420 --> 10:22.870
And it's going to take an input which is the account.

143
10:23.560 --> 10:32.440
And now using this account we can make this more reusable by deleting the _a part of it.

144
10:35.710 --> 10:41.500
And of course we need to indent everything over so that it's actually within this function.

145
10:41.500 --> 10:47.140
And instead of printing the final outcome I'm going to return it as the output.

146
10:47.980 --> 10:51.700
So now we can call that function down here,

147
10:51.700 --> 10:54.910
and we can pass in each account in turn.

148
10:55.600 --> 11:02.930
For example, we could say print, Compare A, which is going to be the first account.

149
11:02.930 --> 11:09.260
So we're going to use our f-string to insert the data that we get back from our function call,

150
11:09.260 --> 11:12.680
and I'm going to call it within the print statement.

151
11:12.680 --> 11:15.890
You can of course take it out and create a separate variable,

152
11:15.890 --> 11:19.130
but I think this should be clear enough to see what's going on.

153
11:19.160 --> 11:20.900
So we're going to call format_data,

154
11:20.900 --> 11:25.220
and we're going to pass in the account_a for this first line,

155
11:25.340 --> 11:28.310
and then I'm going to do the same for B.

156
11:28.310 --> 11:31.220
So in this case I'm going to pass in account_b,

157
11:31.220 --> 11:34.520
and it's going to format the data and then print it out.

158
11:34.520 --> 11:38.570
So as I always say it's a good idea to run your code frequently,

159
11:38.570 --> 11:44.120
probably any time you solve a problem you should probably run it and see if it works the way you expect

160
11:44.120 --> 11:44.750
it to.

161
11:44.780 --> 11:47.390
So the first one, the logo gets printed.

162
11:47.390 --> 11:52.790
The second one, we've managed to get some random accounts because every time I run it, the accounts

163
11:52.790 --> 11:54.290
should be different.

164
11:54.290 --> 12:01.700
And finally, we format the account into this format, with their name, their description, and their

165
12:01.700 --> 12:05.150
country all written out from the print statement,

166
12:05.150 --> 12:09.170
using this lovely format_data() function we've created.

167
12:09.920 --> 12:13.280
We've already done really well, so let's keep going.

168
12:13.550 --> 12:20.300
Now, at the moment it just says compare A, compare B, but instead it'd be much nicer if we can put

169
12:20.300 --> 12:25.700
in that art the Vs ASCII art in between the A and B.

170
12:26.480 --> 12:34.220
Now you could write the line again, 'from art import vs,' but instead, you could also just add a comma

171
12:34.250 --> 12:35.720
here and write vs.

172
12:35.720 --> 12:40.340
So now it's going to import both the logo and that Vs art.

173
12:40.340 --> 12:42.230
And we're going to put it right here.

174
12:42.230 --> 12:44.030
So we're going to use a print statement.

175
12:44.030 --> 12:49.700
And instead of using a string inside double quotes we're just going to write the name of the variable

176
12:49.700 --> 12:51.260
which is called vs.

177
12:51.260 --> 12:56.600
And if we take a look at the final version you can see it also says, Compare A Against B,

178
12:56.600 --> 12:59.910
so we'll change the second word to Against.

179
13:00.510 --> 13:07.080
And now when we run the code, it should look pretty much identical to the starting point of our final

180
13:07.080 --> 13:07.740
game.

181
13:08.340 --> 13:10.350
Let's tackle the next problem.

182
13:10.350 --> 13:15.780
We need to ask the user for a guess to see who has more followers.

183
13:15.810 --> 13:16.440
Right?

184
13:16.440 --> 13:20.940
So I'm actually just going to copy this string right here.

185
13:20.940 --> 13:23.880
And I'm going to put that into an input.

186
13:24.750 --> 13:26.550
"So who has more followers?

187
13:26.580 --> 13:27.960
A or B: "

188
13:27.960 --> 13:31.560
And this input is going to come back as a string,

189
13:31.560 --> 13:35.280
and I'm going to save it inside a new variable called guess.

190
13:35.310 --> 13:40.020
Now because it asks them for A or B, they might type A like this 'a',

191
13:40.020 --> 13:42.060
or they might type A like this 'A'.

192
13:42.090 --> 13:45.660
Ideally, we should make it so that it doesn't matter what they typed,

193
13:45.690 --> 13:53.760
it'll still be valid as long as they typed an A. So we can turn this all into a lowercase format.

194
13:53.790 --> 13:59.320
So if they typed it capital like this, it'll become lower if they typed it lowercase then it's already

195
13:59.320 --> 14:01.570
lower and it will be fine as well.

196
14:02.110 --> 14:04.540
So that way it's more consistent.

197
14:04.780 --> 14:11.890
So now our final product actually looks pretty much exactly identical, but it doesn't really have much

198
14:11.890 --> 14:13.840
gaming functionality yet.

199
14:13.870 --> 14:19.600
To do that, we have to move on to the next problem and check if the user got it right.

200
14:19.660 --> 14:25.990
In order to check if they got it right, the first thing we'll need is to know how many followers they

201
14:25.990 --> 14:27.130
each have, right?

202
14:27.160 --> 14:38.470
Each of the accounts A and B. Let's get an a_follower_count and that is going to be using the account_a

203
14:38.470 --> 14:42.130
and then pulling out the data under the key, follower_count.

204
14:42.160 --> 14:46.180
So I'm actually just going to copy it to make sure I definitely don't make a typo.

205
14:46.270 --> 14:52.510
And I'm going to tap into account_a and then pass in that key, follower_count,

206
14:52.630 --> 14:55.750
remember as a string because our key is a string,

207
14:55.750 --> 15:01.510
so when we use it, it also has to be a string inside double quotes, and I'm going to do the same for

208
15:01.510 --> 15:05.650
the b_follower_count from account_b.

209
15:05.860 --> 15:09.460
Now I've tackled that mini step inside the problem.

210
15:09.460 --> 15:14.170
The next step is to use an if statement to check if the user is correct.

211
15:14.260 --> 15:20.380
Now again, I think this piece of functionality would be really nice if it was reusable.

212
15:20.380 --> 15:28.330
So I'm actually going to pull out of this main, sort of flow of the game and define a separate

213
15:28.330 --> 15:29.800
function at the top.

214
15:29.830 --> 15:33.490
And this function is going to be called check_answer(),

215
15:33.880 --> 15:43.600
and it's going to get, the guess, the a_followers, and the b_followers count,

216
15:45.310 --> 15:48.250
and I'm going to add some docstrings.

217
15:49.630 --> 15:55.420
So the docstrings are really important especially when your function returns something.

218
15:55.420 --> 16:02.290
For example, in this case, it should actually say, """"Takes the account data and returns the printable format."""

219
16:02.290 --> 16:10.180
That makes it so much clearer what this function actually does, what are its inputs and what are its

220
16:10.180 --> 16:16.510
outputs. In our check_answer(), then it's going to take the user's guess and the follower counts of

221
16:16.540 --> 16:20.020
A and B, and then returns if they got it right.

222
16:20.020 --> 16:22.420
So how might we write this?

223
16:22.420 --> 16:25.990
Because the logic is actually a little bit difficult.

224
16:26.020 --> 16:33.250
We need to check which account has more followers, and then we need to check if the user actually got

225
16:33.250 --> 16:33.580
it right.

226
16:33.580 --> 16:35.890
So it's almost a two step process.

227
16:36.370 --> 16:41.410
And I think in these cases, it's actually really worth sort of sketching it out.

228
16:41.440 --> 16:43.990
You can use a pen and a piece of paper,

229
16:43.990 --> 16:47.800
I'm just using Skitch right here, so I can show it to you more easily.

230
16:47.800 --> 16:52.870
So let's just construct a simple sort of two by two matrix.

231
16:53.500 --> 17:03.440
And at the top, we've got the...let's say the user guess is A, and an alternative is they guess B.

232
17:03.920 --> 17:06.410
Now these are the user's guesses,

233
17:06.410 --> 17:13.790
but then it's going to be compared against the actual reality or the answer, which is that A has more

234
17:13.790 --> 17:19.580
followers than B, and the alternative is that B has more followers than A.

235
17:19.610 --> 17:26.540
If the user guessed A and A indeed has more followers than B, then they got it right.

236
17:26.660 --> 17:32.540
And similarly, if they guessed B and B did indeed have more followers than A, then they would have also

237
17:32.540 --> 17:33.710
gotten it right.

238
17:34.010 --> 17:39.470
But if it was the opposite case, as in they guessed A, but actually B had more followers than they

239
17:39.470 --> 17:40.370
would be wrong.

240
17:40.370 --> 17:45.320
And also if they guessed B and A has more followers, then they would be wrong as well.

241
17:45.410 --> 17:50.570
Now using this table, we can now construct our if statement.

242
17:51.440 --> 17:58.130
There is a very, very long version of it, and there is a much shorter version of it. So the very long

243
17:58.130 --> 18:07.160
version would check for something like if the a_followers is greater than the b_followers, so

244
18:07.190 --> 18:13.400
A has more followers than B, and the guess is equal to A.

245
18:13.430 --> 18:21.080
Now you can go through this kind of way and create four of these if and elif, else statements, but it's

246
18:21.080 --> 18:23.960
very easy to get lost in some of the logic.

247
18:23.990 --> 18:30.350
And also it means you have to type a lot of this out instead of using the else as the alternative.

248
18:30.350 --> 18:39.410
So I think a much easier way would be to first check if A has more followers than B, and in this case

249
18:39.410 --> 18:40.970
you can then check,

250
18:40.970 --> 18:45.590
well, if the guess is equal to 'a', well then that means they got it right,

251
18:45.590 --> 18:47.360
so I should return True,

252
18:47.360 --> 18:52.550
but if this was not right then I should return False.

253
18:52.730 --> 19:01.620
An alternative and a much easier way of expressing this is to simply return guess == a, because

254
19:01.620 --> 19:08.610
when this is evaluated, is guess == 'a',  then it's going to return True,

255
19:08.640 --> 19:14.070
but if guess is not 'a', if it's 'b', then it's going to return False.

256
19:14.070 --> 19:16.170
So that's a little bit mind bending,

257
19:16.170 --> 19:21.870
but if you actually go through it enough times, especially using a debugger to just see how it works,

258
19:21.870 --> 19:24.750
then it should become pretty clear pretty quickly.

259
19:25.590 --> 19:34.500
In my case, all I need to do to express my table of possibilities is to first check if A is greater

260
19:34.500 --> 19:35.370
than B,

261
19:35.610 --> 19:37.320
now else,

262
19:37.350 --> 19:44.310
namely, if the opposite was true, B was greater than A, then I'm going to return, user_guess is equal

263
19:44.310 --> 19:45.240
to "b".

264
19:45.300 --> 19:51.660
So now basically the code says, if A has more followers and they guessed A, then return True.

265
19:51.690 --> 19:58.230
If B has more followers and they guessed B, then return True, and if the opposite happens, then return

266
19:58.230 --> 19:59.130
False.

267
19:59.310 --> 20:05.190
Basically, it's going to return whether if the user got the answer right.

268
20:06.030 --> 20:10.320
Now we can basically check that in here.

269
20:10.500 --> 20:19.800
And we could create a new variable called is_correct and set that to equal the output of our check_answer()

270
20:19.800 --> 20:20.700
function.

271
20:20.700 --> 20:26.670
So we're going to call it and pass in all the necessary things, the guess, the followers from A and B.

272
20:26.670 --> 20:33.000
So guess, and then the a_follower_count, and the b_follower_count.

273
20:34.500 --> 20:41.820
Now once we know whether if they got it right or not, we can tackle the next step which is give user

274
20:41.820 --> 20:47.220
some feedback on their guesses. So we can check well if is_correct,

275
20:47.220 --> 20:52.740
well, in this case, we should probably print something like, "You're right!",

276
20:54.060 --> 20:59.380
but if they didn't get it right, then we're going to print,

277
20:59.500 --> 21:00.280
"Sorry.

278
21:00.280 --> 21:01.660
That's wrong."

279
21:02.110 --> 21:09.460
Now, in addition to giving them feedback, we should probably also keep track of the score, so we can

280
21:09.460 --> 21:13.840
start off our game with a score of 0,

281
21:13.870 --> 21:21.640
but as they play on, and if they get it correct, then the score should be incremented by one each time.

282
21:21.640 --> 21:28.210
And in addition to printing out, "You're right," we should also give them some feedback on the current score.

283
21:29.770 --> 21:36.610
And if "Sorry they got it wrong," then we should also give them the final score because at this point

284
21:36.610 --> 21:40.420
the game is going to end and they should know how well they did.

285
21:41.410 --> 21:46.570
So now let's go ahead and play our game and see if it works.

286
21:46.660 --> 21:51.040
So compare Cardi B against Kourtney Kardashian.

287
21:51.070 --> 21:56.380
I think it's probably going to be B and it tells me, "You're right."

288
21:56.380 --> 21:58.910
If we get it right, we get the feedback,

289
21:58.940 --> 21:59.570
"You're right!"

290
21:59.570 --> 22:00.950
and the current score.

291
22:00.980 --> 22:05.030
If we get it wrong, then we get the answer,

292
22:05.030 --> 22:05.720
"Sorry.

293
22:05.720 --> 22:06.590
That's wrong."

294
22:06.590 --> 22:07.790
And the final score as

295
22:07.790 --> 22:11.540
well. That's all working so far.

296
22:11.570 --> 22:16.370
Now we have to figure out some sort of way of making the game repeatable.

297
22:16.400 --> 22:21.920
And as you might remember, that's usually going to require some sort of while loop.

298
22:21.950 --> 22:27.470
But it's important to think about which part of our game actually repeat themselves.

299
22:27.470 --> 22:28.010
Right?

300
22:28.010 --> 22:31.610
Because when we play the game, it looks like everything is kind of static,

301
22:31.610 --> 22:35.420
but if we think about it, the score is set to 0,

302
22:35.420 --> 22:42.080
probably can't be repeated, but the accounts probably do need to be generated and they do need to be

303
22:42.080 --> 22:43.640
randomized again.

304
22:43.640 --> 22:50.060
And then we need to ask the user again Compare A Against B, and then ask them for an input and then

305
22:50.060 --> 22:51.050
check the answer.

306
22:51.050 --> 22:54.350
So all the rest of this probably needs to be repeated.

307
22:54.350 --> 22:58.970
So I would say everything from here probably should be within our while loop.

308
22:59.000 --> 23:02.450
So what is our while loop going to check?

309
23:02.480 --> 23:12.020
Well, maybe we could create a flag like game_should_continue, and start out as True,

310
23:12.020 --> 23:18.290
and then while that's still True, then we'll repeat all of this code below.

311
23:19.370 --> 23:22.790
Now, at which point should this become False?

312
23:22.820 --> 23:29.780
Well, they get to keep going as long as they get the answer right, but the game ends as soon as they

313
23:29.780 --> 23:30.710
get it wrong.

314
23:30.740 --> 23:32.780
This is the point where we're going to change,

315
23:32.810 --> 23:36.380
game_should_continue to False, so that once it prints,

316
23:36.410 --> 23:36.770
"Sorry.

317
23:36.770 --> 23:37.430
That's wrong,"

318
23:37.430 --> 23:39.590
it's going to end the game.

319
23:39.980 --> 23:44.930
Now let's try and run the game and see if we can get our loop to repeat itself.

320
23:45.320 --> 23:50.570
I'm going to choose 'A', and you can see, "You're right! Current score 1."

321
23:50.570 --> 23:58.700
And now it's telling me to Compare A which is a new random person Against B a new random person.

322
23:58.910 --> 24:06.590
As long as I get it right, it will allow me to keep playing the game, but as soon as I get it wrong...

323
24:06.740 --> 24:12.740
So I'm pretty sure that National Geographic sadly doesn't have as many followers as Kim Kardashian,

324
24:12.740 --> 24:16.040
so I think this is going to get me to exit the game.

325
24:16.040 --> 24:16.700
"Sorry,

326
24:16.700 --> 24:17.270
that's wrong.

327
24:17.300 --> 24:22.280
Your final score was 3," and as you can see, my game has now ended.

328
24:22.550 --> 24:29.000
But notice how in the final version of the game, the count at position B actually becomes the one at

329
24:29.000 --> 24:31.190
position A in the next round.

330
24:31.190 --> 24:34.850
So the game kind of continues like this.

331
24:34.850 --> 24:42.410
And then once I guess this, Kim Kardashian is now going to become the one at position A, and it keeps

332
24:42.410 --> 24:44.930
on going as long as I got it right.

333
24:44.930 --> 24:50.000
So we now need to code this functionality into our program.

334
24:50.030 --> 24:57.690
At the very beginning, we generate a random account_a and a random account_b, but instead what we want

335
24:57.690 --> 25:04.290
to happen is we want to make sure that the accounts at position B becomes the next account at position

336
25:04.320 --> 25:04.740
A.

337
25:04.770 --> 25:13.140
So once this game has run its course and we're looping back to the beginning again, the account_a should

338
25:13.170 --> 25:21.030
become the previous version of account_b, but this obviously will give us an error because there is

339
25:21.030 --> 25:22.710
no previous account_b.

340
25:22.740 --> 25:31.770
So what we could do is we could actually take this out of the loop, and say account_b is equal to a random

341
25:31.770 --> 25:41.910
choice() of data, and account_a, becomes that account_b, but then the actual account_b, is going to be

342
25:41.910 --> 25:44.460
a new random account.

343
25:44.490 --> 25:47.700
Let's play this through in our heads as a computer.

344
25:47.730 --> 25:54.030
The first thing that happens when the game starts for the very first time is we generate a random account_b,

345
25:54.060 --> 26:01.710
now that account becomes the account_a, and then account_b becomes a new random account.

346
26:01.740 --> 26:06.300
Now when the game loops back, it's not going to touch these parts of the code.

347
26:06.300 --> 26:08.280
It's going to start from here.

348
26:08.280 --> 26:14.400
So now the account_a is going to be replaced by the previous account_b.

349
26:14.430 --> 26:19.380
And this new account_b is going to be a new random account,

350
26:19.380 --> 26:25.740
and this way when we run our code you can see that as long as I get the answer correct,

351
26:27.810 --> 26:35.850
then the previous B becomes the new A, and B now becomes a new random account.

352
26:37.620 --> 26:41.700
That's pretty much all of the functionality sorted, right?

353
26:41.700 --> 26:44.160
But we've got a couple of things to tidy up.

354
26:44.160 --> 26:48.570
One is we need to clear the screen in between the rounds of our game,

355
26:48.570 --> 26:50.550
but when do we want that to happen?

356
26:50.550 --> 26:52.890
Well, let's take a look at the final game.

357
26:52.890 --> 26:57.250
When we type any sort of answer after we give our input,

358
26:57.250 --> 27:00.190
that's when the screen is supposed to clear.

359
27:00.190 --> 27:05.650
So that means in our code it's going to be right below where we give the input.

360
27:05.650 --> 27:08.590
So here we can clear the screen.

361
27:08.950 --> 27:16.000
And we're going to use that usual trick that we've been practicing, which is just the \n symbol

362
27:16.000 --> 27:19.570
multiplied by 20 times in order to clear the screen.

363
27:19.600 --> 27:25.750
Now the other thing you notice is that every time the screen clears, we get the logo reprinted.

364
27:25.750 --> 27:27.970
So let's go ahead and do that as well.

365
27:27.970 --> 27:32.470
Right after we clear the screen, let's print the logo once more.

366
27:32.500 --> 27:40.660
And now if we rerun our code, we should hopefully have more or less the same functionality as our final

367
27:40.660 --> 27:41.440
demo.

368
27:41.980 --> 27:48.670
And it took quite a bit of code at the end, but as long as you broke it down into these smaller components,

369
27:48.670 --> 27:53.680
each of the components and each of the problems are much, much easier to tackle.

370
27:53.680 --> 27:57.820
I hope you enjoyed building this game and I'll see you on the next lesson.