WEBVTT

0
00:00.710 --> 00:06.470
All right, guys, to get started, head over to the starting file in Step 1,

1
00:06.470 --> 00:10.400
and we're going to tackle this project in five steps.

2
00:10.490 --> 00:14.570
Now in Step 1 you'll see that there are three TODOs.

3
00:14.570 --> 00:20.960
So I want you to go through each of them and complete them in the order they're listed one, two, and three.

4
00:20.990 --> 00:26.780
Now in PyCharm there's a really helpful way to view all the TODOs in a particular file.

5
00:26.780 --> 00:34.440
If you go to View, and then Tool Windows, and then scroll down to TODO, and you'll see all the TODOs

6
00:34.440 --> 00:39.990
listed in order in the current file one, two and three.

7
00:39.990 --> 00:44.670
Now the goal is to be able to achieve what the solution file does.

8
00:44.670 --> 00:48.600
So without opening the solution file, which you need to double-click on,

9
00:48.600 --> 00:53.460
you can also right-click on it and run the file without looking at it.

10
00:53.460 --> 01:01.350
Now once you run it, you can see it's being run down here and it's printed a word,

11
01:01.350 --> 01:09.900
so as we said in TODO Number 1, "Randomly choose a word from the word_list," up here, "and assign it

12
01:09.900 --> 01:12.840
to a variable called chosen_word and then print it."

13
01:12.840 --> 01:14.640
So there it is printed.

14
01:14.640 --> 01:22.330
And then we ask the user to guess a letter, and that's TODO Number 2.

15
01:22.480 --> 01:25.660
And then we have to make the guess lowercase.

16
01:25.660 --> 01:28.480
So let's guess a letter, a,

17
01:29.470 --> 01:35.500
and then finally we're going to check if the letter that the user guessed is one of the letters in the

18
01:35.500 --> 01:40.270
chosen_word and print, "Right" if it is, or print, "Wrong" if it's not.

19
01:40.270 --> 01:46.820
So it's going through each of the letters in this chosen_word, checking each one against the guessed

20
01:46.820 --> 01:47.510
letter.

21
01:47.510 --> 01:50.660
So it's not the first one, but it is the second one.

22
01:50.660 --> 01:53.420
So A does in fact show up here.

23
01:53.420 --> 01:56.720
And then it's also not in the last three letters.

24
01:56.720 --> 02:00.260
So this is the goal of Step 1.

25
02:00.260 --> 02:06.920
And I recommend you go through the TODOs, one by one using everything you've learned in the lessons

26
02:06.930 --> 02:11.160
so far, you should be able to achieve this. If you get stuck,

27
02:11.160 --> 02:17.400
there is a hint down here to tell you how to loop through lists,

28
02:17.700 --> 02:19.860
but we did cover that at one point in the lessons.

29
02:19.860 --> 02:25.410
So you could also go back to that previous lesson where we covered for loops and lists, to help

30
02:25.410 --> 02:26.250
you as well.

31
02:26.790 --> 02:33.480
So I'm going to go silent now and let you pause the video and complete this Step 1.

32
02:46.500 --> 02:46.920
All right.

33
02:46.920 --> 02:49.950
So now let's go through the solution.

34
02:49.950 --> 02:56.470
I'm going to collapse everything just so that I have a bit more space to see each of my TODOs, and

35
02:56.470 --> 03:00.040
alternatively, we can also just open up the TODO window.

36
03:00.130 --> 03:04.210
So the first TODO is to randomly choose a word from the word_list.

37
03:04.210 --> 03:08.560
So to do that we need to import the Random Module.

38
03:08.560 --> 03:15.370
And then using the Random Module, you might remember that we saw that there was a function called random.choice().

39
03:15.370 --> 03:19.000
And inside here we need to pass in a sequence.

40
03:19.000 --> 03:23.390
And in this case, the sequence we want to pass in is our word_list.

41
03:23.420 --> 03:30.890
So now that we've created a random word from that list, we need to assign it to a variable.

42
03:30.890 --> 03:36.800
And in the TODO it says, "to assign it to a variable called chosen_word."

43
03:36.800 --> 03:40.760
So we can just add an equal sign between those two things,

44
03:40.760 --> 03:43.790
and now we have set it to this variable.

45
03:43.880 --> 03:47.820
The final thing we need to do is simply just print the chosen_word.

46
03:47.820 --> 03:51.360
And that's TODO Number 1 completed.

47
03:51.390 --> 03:55.620
Now you might notice there is a yellow squiggly line under the import.

48
03:55.620 --> 04:03.150
And that's because according to PEP 8 and Python style guidance, imports should always be at the very

49
04:03.150 --> 04:04.290
top of the list.

50
04:04.290 --> 04:08.310
But because we've got our TODOs as well, we had it down there.

51
04:08.310 --> 04:09.360
But it would work in

52
04:09.360 --> 04:11.670
both cases exactly the same.

53
04:12.630 --> 04:21.330
So now let's tackle TODO Number 2, which is to ask the user to guess a letter and assign their answer

54
04:21.330 --> 04:23.130
to a variable called guess.

55
04:23.160 --> 04:29.610
So firstly, let's go ahead and figure out how to ask the user for a letter.

56
04:29.610 --> 04:31.590
So we're going to use input.

57
04:31.590 --> 04:37.720
And I'm simply just going to write, "Guess a letter..." colon, space.

58
04:37.720 --> 04:43.360
And then we're going to assign this to a variable called guess.

59
04:44.110 --> 04:47.620
And finally, we want to make this guess lowercase.

60
04:47.620 --> 04:51.220
So we can simply write, .lower().

61
04:52.270 --> 04:59.540
Now let's go ahead and print the value of guess and run our code, just to make sure that everything

62
04:59.540 --> 05:02.300
works so far as we expect it to.

63
05:02.330 --> 05:07.670
So instead of selecting solution, I'm going to select current file and hit Run.

64
05:07.700 --> 05:10.610
So the first thing that prints is, baboon.

65
05:10.610 --> 05:17.780
And this means that our TODO 1, was completed successfully because we randomly chose a word from the

66
05:17.780 --> 05:20.270
word_list and then printed it out.

67
05:21.110 --> 05:28.760
Now TODO 2 should ask the user to guess a letter and then go ahead and make it lowercase.

68
05:28.760 --> 05:31.580
So we're printing it out just to make sure it works.

69
05:31.580 --> 05:38.240
Let's say I guess the letter, but I type a capital A, now when I hit enter, hopefully this .lower()

70
05:38.240 --> 05:40.850
function will turn it into lowercase,

71
05:40.850 --> 05:46.370
and then it gets stored in this variable called guess, which gets printed out here.

72
05:46.380 --> 05:54.240
Now the reason why we're turning it to lowercase is because the user might start off a letter in all caps,

73
05:54.240 --> 05:56.280
or they might use lowercase,

74
05:56.280 --> 06:02.790
but if we were to check against our words in the word_list, you'll notice that they're all in lowercase.

75
06:02.790 --> 06:08.160
So if we make everything all the same casing, that makes our lives a lot easier down the line.

76
06:08.580 --> 06:14.830
So we have now completed TODO 1 and TODO 2, and all that's left to do is TODO Number 3.

77
06:15.550 --> 06:22.240
So what we want to do is we want to take this letter that the user guessed, A, check it against each of

78
06:22.240 --> 06:29.950
the letters in the word that was randomly chosen, and see if it matches any of these letters.

79
06:29.950 --> 06:36.710
So I would expect the first letter to be a wrong because A is not equal to B.

80
06:36.740 --> 06:40.070
The second one I would expect it to print right.

81
06:40.070 --> 06:44.330
And for the rest of the letters I would expect it to print wrong.

82
06:44.330 --> 06:52.070
So if you remember, we can use a for loop to loop through things which are in a sequence.

83
06:52.070 --> 06:53.840
And we can do that with lists.

84
06:53.840 --> 06:56.420
But we can also do that with strings.

85
06:56.420 --> 07:03.410
So we can basically use the same for-in loop that we learnt about previously, where we say 'for' a letter,

86
07:03.410 --> 07:09.800
so this is a variable name that we give each of the letters in the chosen_word that we're going to loop

87
07:09.800 --> 07:10.100
through.

88
07:10.100 --> 07:11.900
You can call it anything you want.

89
07:11.900 --> 07:15.470
And then we use 'in' to define what are we looping through.

90
07:15.470 --> 07:18.380
So it's going to be the chosen_word.

91
07:18.380 --> 07:23.810
And let's go ahead and just print letter just to make sure this is doing what we expect it to.

92
07:23.820 --> 07:29.670
So let's finish off with the input and you can see what's happening here is it's looping through each

93
07:29.670 --> 07:34.020
of the letters in this chosen_word, which in this case was aardvark,

94
07:34.020 --> 07:36.330
and it's printing it one by one.

95
07:36.930 --> 07:48.000
Now instead of printing the letter, we want to check to see if the letter is the same as the guess.

96
07:48.010 --> 07:55.810
So if the letter that we're currently looking at in the chosen word A, is it the same as the guess

97
07:55.810 --> 07:57.610
that the user inputted?

98
07:57.640 --> 08:02.470
Well, if it is, in that case we're going to print, "Right,"

99
08:02.470 --> 08:09.520
and if it's not, so in the case of else, making sure you've got it at the right indentation level, notice

100
08:09.520 --> 08:12.250
how if and else are at the same starting line.

101
08:12.250 --> 08:15.530
Then in this case we're going to print "Wrong."

102
08:15.560 --> 08:17.900
So now let's rerun our code.

103
08:17.900 --> 08:20.750
In this case the word that was chosen was camel.

104
08:20.750 --> 08:23.000
And I'm still guessing the letter A.

105
08:23.120 --> 08:25.220
Then it goes through this word, camel,

106
08:25.220 --> 08:27.440
the first letter is c, c is not a.

107
08:27.440 --> 08:28.520
So that's wrong.

108
08:28.520 --> 08:30.110
Second letter is a,

109
08:30.140 --> 08:31.730
so it's right.

110
08:31.730 --> 08:33.800
And none of the other letters are a,

111
08:33.800 --> 08:35.270
so it prints out wrong.

112
08:35.270 --> 08:40.010
So this was what we wanted to achieve by the end of Step 1.

113
08:40.610 --> 08:42.800
Did you manage to get it to work?

114
08:42.830 --> 08:44.540
If not, don't worry.

115
08:44.540 --> 08:46.490
Go back to your code, fix it.

116
08:46.490 --> 08:53.180
And as long as you understand what's going on, and how the code works in your version, then that is

117
08:53.180 --> 08:55.010
a success in my book.

118
08:55.040 --> 09:01.100
Now, once you're ready, then head over to Step 2, and in the next video I'll describe the steps

119
09:01.100 --> 09:02.630
in that section.