Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:01,240 --> 00:00:07,990
Now, if you head over to the course resources, you'll be able to find a link to this first code exercise.
2
00:00:08,560 --> 00:00:15,010
And the idea is that you're going to create basically a virtual coin-toss program so that when you run
3
00:00:15,010 --> 00:00:21,360
the code, it should give you tails or heads depending on which random number was generated.
4
00:00:22,310 --> 00:00:26,280
Try to use what you've learned in the previous lesson to complete this challenge.
5
00:00:26,680 --> 00:00:28,810
So pause the video now and give that a go.
6
00:00:30,050 --> 00:00:32,270
All right, let's go through the solution together.
7
00:00:32,870 --> 00:00:39,110
So first, make sure you've forked your copy of the exercise so that it's under your own username, and
8
00:00:39,110 --> 00:00:42,490
then we're going to go to main.py where we always write our code.
9
00:00:43,160 --> 00:00:46,850
Now, in order to use the random module, we first have to import it.
10
00:00:47,780 --> 00:00:49,370
So the module is called random.
11
00:00:49,370 --> 00:00:57,710
So we write import random. And now using the random module, we can now tap into randint, which will
12
00:00:57,710 --> 00:01:03,580
generate a random integer between a starting number and a ending number
13
00:01:03,830 --> 00:01:06,290
and it's inclusive of both numbers.
14
00:01:06,860 --> 00:01:10,250
So let's call this the random_side.
15
00:01:12,030 --> 00:01:18,150
And using this random_side, we can use an if statement to check well if the random side that was
16
00:01:18,150 --> 00:01:26,490
generated is equal to one, well, in that case, that means we should print that we generated heads.
17
00:01:26,760 --> 00:01:29,880
So that's the part of the coin which normally shows a person's face.
18
00:01:30,600 --> 00:01:38,440
Now we can use else to catch the situation where we get a zero, and in this case, we'll print tails instead.
19
00:01:39,180 --> 00:01:41,040
So that's all there is to it.
20
00:01:41,220 --> 00:01:45,710
And of course, as always, there are many, many ways that you could solve this challenge.
21
00:01:45,960 --> 00:01:50,430
And if you're creative or if you looked around and you found a different way, then that's great.
22
00:01:50,610 --> 00:01:54,510
But this is how we would do it using what we learned in the previous lesson.
23
00:01:55,410 --> 00:02:00,570
So how did you get on with it? If you got stuck or if you forgot how to do certain things or if some
24
00:02:00,570 --> 00:02:06,720
parts of your code didn't work or if you had errors, then this is the time to go back and fix it so
25
00:02:06,720 --> 00:02:09,690
that you actually understand what's going on and we can proceed.
26
00:02:09,990 --> 00:02:12,420
So once you're done, head over to the next lesson
27
00:02:12,420 --> 00:02:15,300
and we're going to learn about Python lists.
3139
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.