[go: up one dir, main page]

0% found this document useful (0 votes)
51 views3 pages

A Short Program: Guess The Number:: "I Am Thinking of A Number Between 1 and 20."

The program generates a random number between 1 and 20 for the player to guess. It allows the player 6 guesses to try to match the secret number. After each guess, it tells the player if their guess is too high or too low. If they guess correctly within 6 tries, it congratulates them. Otherwise, it reveals the secret number.

Uploaded by

fRiOsS Rios
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
51 views3 pages

A Short Program: Guess The Number:: "I Am Thinking of A Number Between 1 and 20."

The program generates a random number between 1 and 20 for the player to guess. It allows the player 6 guesses to try to match the secret number. After each guess, it tells the player if their guess is too high or too low. If they guess correctly within 6 tries, it congratulates them. Otherwise, it reveals the secret number.

Uploaded by

fRiOsS Rios
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
You are on page 1/ 3

A short program: Guess the number:

#This is a guess the number game.


import random

secretNumber = random.randint(1, 20)

print("I am thinking of a number between 1 and 20.")

#As the player to guess 6 times.


for guessesTaken in range(1, 7):

print("Take a guess.")

guess = int(input())

if guess < secretNumber:

print("Your guess is too low.")

elif guess > secretNumber:

print("Your guess is too high.")

else:

break #the condition is the correct guess!

if guess == secretNumber:

print("Good Job!, You guessed my number in: ", guessesTaken, "guesses!")

else:

print("Nope. The number I was thinking of was: ", secretNumber)


You can view the execution of this program at: https://autbor.com/guessthenumber/. Let’s look the code
line by line, starting at the top.
First a comment at the top of the code
#This is a guess the number game.
explains what the program does.

import random
The program imports the random module...

...so that it can use the random.randint() function to


secretNumber = random.randint(1, 20)
generate a number for the user to guess.

The return value, a random integer between 1 and 20,


is stored in the variable secretNumber

######################################################

The program tells the player that it has come up with a secret number and will give
the player six chances to guess it.

print("I am thinking of a number between 1 and 20.")

#As the player to guess 6 times.


The code that lets the player enter a guess and check that
for guessesTaken in range(1, 7):
guess is in a for loop that will loop at at most six times
print("Take a guess.")
The first thing that happens in the loop is
guess = int( input() )
that the player types in a guess.

Since input returns a string

This gets stored in a variable named Its return value passed a straight into int(), which translate the string
guess. into an integer value.

######################################################
if guess < secretNumber: These few lines of code check to see whether the guess is
less than or greater than the secret number
print("Your guess is too low.") (secretNumber). In the either case, a hint is printed to the
scree.
elif guess > secretNumber:

print("Your guess is too high.")

######################################################

else:

break #the condition is the correct guess!

If the guess is neither higher or lower than the secret number, then it must be equal to the
secret number – in which case, you want the program execution to break out of the for loop.

######################################################

if guess == secretNumber:

print("Good Job!, You guessed my number in: " + str( guessesTaken )+ "guesses!")

else:

print("Nope. The number I was thinking of was: " +str( secretNumber ))

After the for loop, the previous if...else statement checks


whether the player has correctly guessed the number and
appropriate message to the screen.
Since in must concatenate these integer values to
strings, it passes these variables to str() function,
which returns the string value from of the In both cases, display a variables that
integers. Now these string can be concatenate contains an Integer value (gussesTaken
with the + operators before finally being passed to and secretNumber).
the print() function call.

You might also like