import random
#Initial message when running the code
print("Welcome to the number guessing game! Please input a number.")
#This line generates a random number between 1 and 20 to be guessed by the user
target_number = random.randint(1, 20)
#This variable hold the value of how many attempts the use takes. It starts at 0 attempts
attempts = 0
#Loop for the game
while True:
# Lets the player input their guess
try:
guess = int(input("Guess the number: "))
print(f"You guessed: {guess}")
# Rest of your code for processing the guess
attempts += 1
except ValueError:
print("Please enter a valid integer.")
continue
# Check if the guess is correct
#f-strings let you embed variables and expression inside
if guess == target_number:
print(f"Congratulations! You guessed the number {target_number} in {attempts} attempts.")
break
elif guess < target_number:
print(f"{guess} is too low. Try again.")
else:
print(f"{guess} is too high. Try again.")
Welcome to the number guessing game! Please input a number.
You guessed: 6
6 is too low. Try again.
You guessed: 10
10 is too low. Try again.
You guessed: 19
19 is too low. Try again.
You guessed: 20
Congratulations! You guessed the number 20 in 4 attempts.