Number Game Introduction

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.