Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Start a free Basic trial
to watch this video
It's time to build a game. A game where you guess a random number.
Using code from the Python standard library is one of the best things about Python. The standard library offers a huge number of packages for us to take advantage of, which saves us all time and trouble, since the standard library is very heavily tested.
We bring in outside code with the import
keyword and then the name of the library we want to import. There are a few different ways to import code but we'll just stick with importing an entire library for now.
import random
will bring in the random
library and we'll have access to everything in the random
library by using dot notation. For example, the randint
function belongs to the random
library. If we want to use it, we do random.randint()
.
-
0:00
[MUSIC]
-
0:04
Python is sometimes described as a batteries included language.
-
0:09
This is because Python gives you a ton of tools and
-
0:12
functionality right out of the box.
-
0:14
All of these tools collectively make up the Python Standard Library.
-
0:18
There are tools for working with CSV files, creating calendars or
-
0:22
any number of other things.
-
0:23
The Standard Library is huge.
-
0:26
We bring these tools into our scripts using the import keyword.
-
0:31
There are lots of ways to use the import keyword, but
-
0:34
we're going to stick to a simple use of it in this script.
-
0:37
I want to build a game.
-
0:38
You probably played this when you were a kid.
-
0:40
You'd pick a number and a friend tries to guess it.
-
0:43
You tell them if they're too high or too low, and they guess again.
-
0:46
Maybe they have to guess it in five tries or something.
-
0:49
In this app though, the computer will be the one that picks the number and
-
0:52
we will have to guess it.
-
0:54
We'll be using the random library from the Python Standard Library.
-
0:58
Let's go to Workspaces and see what we can come up with.
-
1:01
So let's talk about how to use pieces from the Standard Library.
-
1:06
We have to use the import keyword and
-
1:09
then the name of the library that we want to import.
-
1:13
We're gonna do all of this script in a file named number_game.py.
-
1:19
And so, this is where we need the random library.
-
1:22
So we'll import random.
-
1:25
Now, we have to use the import keyword and
-
1:28
then the name of the library that we want to import.
-
1:30
We want to be able to have random numbers and stuff like that, so
-
1:33
we're gonna use the random library.
-
1:36
This library gives us a lot of possibilities.
-
1:38
I wanna just show you, just for a second.
-
1:41
Python.org, go to documentation, Python 3 Docs.
-
1:47
Over here on the side, we're gonna search for random.
-
1:52
And this right here is the random module.
-
1:54
Okay, this is what we imported.
-
1:57
So if we go look here, here's all the stuff that we get from random.
-
2:01
There's a lot of stuff, I mean there's a good 20 or so methods,
-
2:06
functions, however you wanna think about them that Python gives us.
-
2:10
And we're only gonna use one of these right now which is, where did it go?
-
2:16
Right here, randint.
-
2:19
We're gonna use that one later.
-
2:21
But there's a huge amount of stuff in here.
-
2:22
I highly, highly recommend that you go and look up the random library to see
-
2:27
what all stuff is available in this one handy, little library.
-
2:31
Okay, let's go back to our game.
-
2:34
Let's outline what we're gonna do.
-
2:36
It's always a good idea to plan things out first.
-
2:38
So we want to generate a random number between one and ten.
-
2:44
We want to get a number guess from the player.
-
2:49
We want to compare guess to secret number.
-
2:53
And we want to print a hit or a miss.
-
2:57
So if they get it right, tell them.
-
2:59
If they get it wrong, tell them.
-
3:01
So let's start at the top again.
-
3:06
In a lot of languages, when you wanna get a random number,
-
3:10
you get a random number and it gives you a float somewhere between zero and one.
-
3:14
And then you multiply that by whatever your target number is.
-
3:17
Like, hey, I want between one and
-
3:18
ten, so give me a random number and then we're gonna multiply it by ten.
-
3:24
And then you would round it up, or round it down, or just round it in general and
-
3:29
that gives you your random number.
-
3:31
Now that's a lot of work, way more work than Python expects you us to do.
-
3:36
Python's like I've got your back, don't you worry, I'm gonna handle this for you.
-
3:41
Python's random library gives us a function named randint,
-
3:44
the one that I pointed out a while ago.
-
3:46
And what randint does is it generates a random number
-
3:49
between the two numbers that we give it, including them.
-
3:53
So let's set up our secret numbers.
-
3:55
So we'll say secret_num = random.randint.
-
4:00
And then inside here we have to put what our two maximums, or our maximum and
-
4:05
our minimum, rather minimum and maximum, that's the order they come in.
-
4:08
What those two things are.
-
4:09
So our minimum is one.
-
4:10
It can't be lower than one.
-
4:12
And our maximum is ten.
-
4:13
It can't be higher than ten.
-
4:15
So we have all the numbers between one and ten, including one and
-
4:19
ten as possibilities.
-
4:21
Okay, so we've got our random number.
-
4:24
Now let set up our game loop.
-
4:29
Wow, game loop, all right, so hard.
-
4:32
[LAUGH] I love when things are simple.
-
4:35
Okay, so we're gonna do these three things inside of our game loop.
-
4:41
So we need to get our user input.
-
4:43
So let's say guess = int (input("Guess
-
4:48
a number between 1 and 10:")).
-
4:53
And then we need to compare the guess to the secret number.
-
4:56
So if guess = secret_num: then we're gonna print ("You got it!
-
5:05
My number was {}".
-
5:08
And then let's tell them what the number was just in case they can't
-
5:11
read what they just put in.
-
5:12
We'll say (secret_num).
-
5:14
And then, since we want to end the while loop,
-
5:17
we wanna stop the loop right then, we're gonna do a break.
-
5:22
And otherwise, they didn't get it, so,
-
5:27
this is where you're doing the hit or miss thing here.
-
5:29
We're gonna print, (That's not it!").
-
5:33
All right, so, cool.
-
5:36
This is a super basic game.
-
5:38
I mean it's there, let's try it.
-
5:40
Let's see if it works.
-
5:42
So we'll do python number_game.
-
5:44
Guess a number between 1 and 10.
-
5:46
Let's split the difference, let's say 5.
-
5:49
That's not it.
-
5:50
Okay, let's try 2.
-
5:51
That's not it either, let's try 8.
-
5:55
That's not it.
-
5:58
9, 10, 7, 6, 4 you got it but [INAUDIBLE] all right.
-
6:06
That's really hard and not super fun to play.
-
6:09
I'm sure though that we can make it better.
-
6:12
That's a good start but not exactly the most fun game to play.
-
6:15
See what improvements you can add on your own and
-
6:18
we'll try to add some more functionality and friendliness to it in the next video.
You need to sign up for Treehouse in order to download course files.
Sign up