Guess the number
Python Exercises
Write a Python function named guesTheNumber() that lets the player guess a secret number until they find it!
đInstructions
Goal: Write a Python function named guesTheNumber() that lets the player guess a secret number until they find it!
1. Import the random module
import random
2. Create the secret number and points variable
num = random.randint(1, 200)
3. Start defining your function
def guesTheNumber():
4. Start a loop so the player can keep guessing
while True:
5. Ask the player to guess
guess = int(input("Guess the number: "))
6. Add the main conditions
if guess == num:
print(f"You found it! The number is {num}")
print("Now let's do some math!")
tryMath()
break
elif num - 10 < guess < num:
print("You are close! Try a slightly greater number.")
elif num < guess < num + 10:
print("You are close! Try a slightly smaller number.")
elif guess < num:
print("Itâs more than the number you guessed.")
else:
print("Itâs less than the number you guessed.")
print(f"You found it! The number is {num}")
print("Now let's do some math!")
tryMath()
break
elif num - 10 < guess < num:
print("You are close! Try a slightly greater number.")
elif num < guess < num + 10:
print("You are close! Try a slightly smaller number.")
elif guess < num:
print("Itâs more than the number you guessed.")
else:
print("Itâs less than the number you guessed.")
7. Run the game
guesTheNumber()
Extra Challenges:
import random
randomNumber = random.randint(1, 1000)
points = 0
guessCount = 0
print("â¨Welcome!â¨")
print("Try to guess the numberâď¸đ˘")
print("Youâll get points if your guess is close, and lose points if itâs too far.")
def guesTheNumber():
global points, guessCount
while True:
guess = int(input("Guess the number: "))
guessCount = guessCount + 1
if guess == randomNumber:
print(f":đ You found it! The number is {randomNumber}")
print(f"Total Guesses: {guessCount}")
print(f"Total points: {points}")
break
elif randomNumber - 10 < guess < randomNumber:
points = points + 1
print("You are close. Try a slightly greater number.")
elif randomNumber < guess < randomNumber + 10:
points = points + 1
print("You are close. Try a slightly smaller number.")
elif guess < randomNumber:
points = points - 1
print("Itâs more than the number you guessed.")
else:
points = points - 1
print("Itâs less than the number you guessed.")
guesTheNumber()
randomNumber = random.randint(1, 1000)
points = 0
guessCount = 0
print("â¨Welcome!â¨")
print("Try to guess the numberâď¸đ˘")
print("Youâll get points if your guess is close, and lose points if itâs too far.")
def guesTheNumber():
global points, guessCount
while True:
guess = int(input("Guess the number: "))
guessCount = guessCount + 1
if guess == randomNumber:
print(f":đ You found it! The number is {randomNumber}")
print(f"Total Guesses: {guessCount}")
print(f"Total points: {points}")
break
elif randomNumber - 10 < guess < randomNumber:
points = points + 1
print("You are close. Try a slightly greater number.")
elif randomNumber < guess < randomNumber + 10:
points = points + 1
print("You are close. Try a slightly smaller number.")
elif guess < randomNumber:
points = points - 1
print("Itâs more than the number you guessed.")
else:
points = points - 1
print("Itâs less than the number you guessed.")
guesTheNumber()