Bummer! This is just a preview. You need to be signed in with a Treehouse account to view the entire video.
The Challenge1:59 with Kenneth Love
To gain your Python badge, you must complete a task. **Bring me a shr--* No, wait, wrong task. Let's talk about what you're going to build and how to do imports in Python.
Now that you know how to import libraries, see if you can import the functions you wrote in
function_list.py. You don't need the
.py in the
The requirements for the number game are:
- Players get 5 chances
- They have to guess a random number
- It has to be a whole number from 1 to 10
- If they guess wrong, tell them whether the chosen number is higher or lower than their guess
- Tell them how many guesses they've made
import - A keyword that brings outside code into your script.
Before I turn you loose on making a game, I have to show you something new.
Python is a batteries included language, which means that Python
itself gives you lots and lots of tools to use.
We call this the Python standard library.
You use those tools in your scripts by importing them using the import keyword.
For the guessing game, you're gonna need to use the random library.
Let's look at how we import it and use it.
To import something, we just use the import keyword.
So let's import random.
Since no messages came back, that means it worked.
So, inside of the random library, which we can look at if we do dir
random, we see that there is a randint item.
Let's find out what that does.
So randint takes two numbers, A and B, and gives
us a random number between them, or optionally including them.
So, if we ask for a random integer between one and ten, it could be
any of the numbers between one and ten, or the number one or the number ten.
Let's try using that.
Let's call random.randInt.
Let's do exactly that.
Let's do one and ten.
And we get the number 8.
Let's try something larger.
Let's do one and 100.
And we get 77.
So, here's your assignment.
Make a game that picks a random number between
1 and 10, and asks the user for a number.
If it's not the correct number, tell them whether their guess
is too high or too low, and let them guess again.
If it is the right number, congratulate them.
If you want some extra credit, limit their number of guesses.
Tell them how many guess they've made, or both.
The version I'll show in the next video will do both
of these things, but see if you can beat me to it.
You need to sign up for Treehouse in order to download course files.Sign up