How To Number Guess Game - Python
A number guessing game is one is beginner's small project.
This tutorial will show you how to archeive that in python.
First import the random module
# Generate a random number between 1 and 9 as the secret number
secret_number = randint(1, 9)
user_guess = 0
is_correct = False
print("<<< Number Guessing Game >>>")
print("You have only 3 tries to get the correct number")
print("The secret number is between 1 and 9, Good luck!")
for i in range(3):
user_guess = int(input("Guess> "))
if user_guess == secret_number:
is_correct = True
break
if is_correct:
print("You win!")
else:
print(f"You failed! Secret number is {secret_number}")
In the above code we set the secret_number varaible to a randomly generated number by the randint function, This number is between 1 and 9. Then we print out some describtion and then enter a loop, the loop only terminates if the user guesses the correct number or quess limit is reached.