Guess the Number
Budget: €8 – €30 EUR
In this game, the player has to guess a secret number between 1 and 100 in a certain number of attempts. The code generates a random secret number using the random module and then prompts the player to make a guess using the input() function. The program then checks the guess against the secret number and gives feedback to the player. If the player runs out of attempts, the game ends and the secret number is revealed.
import random
secret_number = random.randint(1, 100)
attempts = 0
max_attempts = 5
print("I'm thinking of a number between 1 and 100. Can you guess it in 5 attempts?")
while attempts < max_attempts:
guess = int(input("Enter your guess: "))
attempts += 1
if guess == secret_number:
print(f"Congratulations! You guessed the number in {attempts} attempts.")
break
elif guess < secret_number:
print("Your guess is too low.")
else:
print("Your guess is too high.")
else:
print(f"Sorry, you ran out of attempts. The secret number was {secret_number}.")
import random
secret_number = random.randint(1, 100)
attempts = 0
max_attempts = 5
print("I'm thinking of a number between 1 and 100. Can you guess it in 5 attempts?")
while attempts < max_attempts:
guess = int(input("Enter your guess: "))
attempts += 1
if guess == secret_number:
print(f"Congratulations! You guessed the number in {attempts} attempts.")
break
elif guess < secret_number:
print("Your guess is too low.")
else:
print("Your guess is too high.")
else:
print(f"Sorry, you ran out of attempts. The secret number was {secret_number}.")