10-Guessing Game Challenge - Solution-Copy1
10-Guessing Game Challenge - Solution-Copy1
The Challenge:
Write a program that picks a random integer from 1 to 100, and has players guess the number.
The rules are:
1. If a player's guess is less than 1 or greater than 100, say "OUT OF BOUNDS"
2. On a player's first turn, if their guess is
within 10 of the number, return "WARM!"
further than 10 away from the number, return "COLD!"
3. On all subsequent turns, if a guess is
closer to the number than the previous guess return "WARMER!"
farther from the number than the previous guess, return "COLDER!"
4. When the player's guess equals the number, tell them they've guessed correctly and how
many guesses it took!
First, pick a random integer from 1 to 100 using the random module and assign
it to a variable
Note: random.randint(a,b) returns a random integer in range [a, b] , including both
end points.
In [1]:
import random
num = random.randint(1,100)
In [2]:
print("WELCOME TO GUESS ME!")
print("If your guess is more than 10 away from my number, I'll tell you you'r
print("If your guess is within 10 of my number, I'll tell you you're WARM")
print("If your guess is farther than your most recent guess, I'll say you're
print("If your guess is closer than your most recent guess, I'll say you're g
print("LET'S PLAY!")
If your guess is more than 10 away from my number, I'll tell you you're COLD
If your guess is farther than your most recent guess, I'll say you're getting
COLDER
If your guess is closer than your most recent guess, I'll say you're getting
WARMER
LET'S PLAY!
In [3]:
guesses = [0]
Write a while loop that asks for a valid guess. Test it a few times to make
sure it works.
In [4]:
while True:
continue
break
Write a while loop that compares the player's guess to our number. If the
player guesses correctly, break from the loop. Otherwise, tell the player if they're
warmer or colder, and continue asking for guesses.
Some hints:
In [5]:
while True:
continue
if guess == num:
guesses.append(guess)
if guesses[-2]:
print('WARMER!')
else:
else:
print('WARM!')
else:
print('COLD!')
COLD!
WARMER!
WARMER!
COLDER!
WARMER!
COLDER!
WARMER!
COLDER!
In the next section we'll learn how to turn some of these repetitive actions into functions that can
be called whenever we need them.
Good Job!