Build a randomly generated guessing game in Python using user inputs by importing the Random Module!

LeMaster Tech
183 followers
39 Video Views·Apr 22, 2025
Python's random module allows you to specify a range of values to pull a number from and will randomly select one! This is a great tool to use for games where an element of randomness can add fun and difficulty!
This video project shows user inputs, building reusable functions, tracking total attempts and modifying the output statement to display how many attempts were required to get the correct answer! This is a great introduction to python's random functions and a good beginner project for those learning python!
Let us know if you have any comments or want to see anything specific in the comments! thanks for checking out the channel and be sure to like and subscribe for more content coming out of the channel!
hello welcome back to the channel uh
today is going to be a continuation in
the python tutorial series on this
channel and today we're going to take a
look at importing modules specifically
today we're going to import the random
module and we're going to use it to
build a random number guessing game so
if you have any questions about anything
you see today feel free to let me know
about in the comments i will be covering
a lot fairly quickly and assume that you
know some of the basics that have been
covered previously in getting started
with python so let's get right into it
to pull in the random package you can't
just call a random number from python
without pulling in the random package so
you're gonna use the python keyword
import and then say what you're gonna
import and we're gonna import the
package random um if for whatever reason
python didn't install on your machine
with random on it you may need to run
pip to install random
but a standard install of python will
use it so it stays grayed out typically
until you're actually calling it
somewhere below
so we'll go ahead and make a
variable and we'll say random number
is going to be equal to and then here's
where you call the random package we'll
say random.rand
and we'll pull in a random
a random integer
so we'll say we want a random integer
between 0 and 100 and what that means is
when this code starts up it's going to
go out and it's going to pull a random
number between
0 and 100
and let's
also as part of our game let's count how
many guesses it takes to guess it
correctly so we'll create a guesses
variable and we'll set it equal to zero
and then we'll also create a variable
called guess
answer so this will be what the user
inputs and we'll start that out at zero
as well so those should be all of the
variables we need as we get into this
um
so let's go ahead and take a look at
actually
creating the function so let's say the
actual action of guessing will create a
little function and we'll call it
we'll call it user
guess
and it doesn't need to pass any
variables in
so inside of user guess we'll say that
the actual guess that you make is an
integer
of uh input and the input that we'll say
is guess a
integer value between
0 and 100 right so we tell the user
right here kind of what the range
they're guessing in should be
and then inside of this guess as well
um
no we'll do the incrementing the guesses
outside of this okay so let's just go
ahead and say we're gonna return the
guess value
and then what we want to do is say
while
the
guess
answer
is not equal to so what we're checking
is that the guess answer is not equal to
our random number that we pulled in in
the beginning so the whole time they're
not equal we then want to check if our
guess
answer
is greater than the random number right
so if the guess answer is greater than
the random number
then we're going to print on the screen
go lower because our guess was too high
and then what we'll do is we'll
increment our guesses so we'll say
guesses plus one
and then we will run the guess function
so or a user guess function so
every time they guess and they don't get
it right we add one to the guesses total
um and then we run the function again so
that's why i put that code in a function
rather than putting it
in here directly so we can just call the
function over and over again and then
we'll say well if it wasn't greater than
and it wasn't equal to which we check in
the beginning we'll say that we're going
to print
go
higher because now the only thing that
it could be still is if we're
too low
and so we'll copy the other two rungs of
code though we're going to increment our
guesses still and then we'll run the
user
guess function um
and then that should be all we need
inside of our while loop and then once
we exit the while loop we're going to
print
uh
we'll we'll say you
got it
actually we'll say you got it in
and then we'll tell them how many tries
it took
in plus string conversion right of our
guesses variable
and then we'll say you got it in that
many tries so one other thing we want to
do this code would run but it's always
gonna make your guesses total one too
high because it thinks you're starting
from zero guesses um so what we'll
actually do is we'll go ahead and run
um
user guess before we even enter the
while loop so this way uh the first
thing that'll run is that you take a
guess and then after that you're in the
while loop so let's go ahead and just
check this out i know i threw a lot of
code in there real quick um
but the random function is kind of cool
and this is just a neat little project
you can use and incorporate in anything
like games
to create a little random number
generation so let's guess an integer
between 0 and 100 let's start with 50.
wants us to go higher let's go to 60
keep going higher we'll go 75 higher
we'll go to 88
higher we'll try 95
higher i guess we'll do 99
and it wants us to go higher
it wants us to go higher than a hundred
that doesn't seem right
go higher let's try one thousand am i
crazy
go higher
something funky's up
if the answer is greater than the random
number tell us to go lower
well the answer is not equal to random
number
else
let's just say
else
we'll do an l if
guess
answer
is lower than
random number just to make sure i'm not
going crazy here because this
strange things are a foot
so let's stop it and re-run it real
quick
so i think we got pretty close
let's try 50 again
okay go higher i've just heard that
before 75. let's go straight to 99.
100.
oh
all right let's take a look at our code
here random number
equals random.rand in
0 to 100.
guesses are zero
yes answer equals
let's try getting rid of oh it's because
that initial guess uh we don't
we don't set it equal to anything boy oh
boy that was silly
so uh what you need to do every time you
call that was stupid every time you call
this function you have to actually
update
your guess answer so we were starting it
at zero and even though we were running
the function we were never
we were never updating it so it was
always saying
uh that the answer was zero so let's try
that again fifty
fifty
go lower if that's nice to see 23 go
higher
40
uh go lower okay let's try 30
let's try 25
higher 27
higher 29
28.
there we go we got in seven tries so uh
let's go ahead and try that um one more
time just see if we can do any better
i'll start right in the middle 50 go
lower 25
lower 12.
higher
18
lower 15.
four tries okay we did a lot better now
the code is working um if you have any
questions about why that wasn't working
i know i said you have to set your
function equal to the actual variable
that you're checking so we were never
updating the guess when i originally
wrote that that's why it messed up
those sorts of little things can cause
hiccups it's always frustrating but this
is a cool little intro to using the
random function and building kind of a
number guesser so hopefully you found
that useful uh sorry i got tripped up
there but if you have any questions
about anything you saw today let me know
about in the comments below if you did
find it useful or anything else on the
channel useful i really appreciate a
like on the video and a subscribe to the
channel helps me out a ton and as always
good luck with your code and thanks for
watching thanks bye
