Number guessing game

In this video, I guide you step-by-step on how to create a fun and entertaining Number Guessing game in Python! The code is below. Hope you like it!


import random # Importing libraries


number = random.randint(1, 10) # Make a variable that stores the computer's random chosen number


guesses = 0 # Set a variable that keeps track of how many guesses you have left


print("I am guessing a number between 1 and 10...") # Let the user know that they can start guessing the computer's number

while guesses < 5: # A while loop that counts how many guesses you have

guess = int(input("Guess! ")) # The user's guessed number is stored in the variable "guess"

guesses += 1 # 1 guess is gone, 4 guesses left

if guess < number:

print("Your guess is lower than my number") # Tell the user the number they picked was lower than the computer's number

if guess > number:

print("Your guess is higher than the my number") # Tell the user the number they picked was higher than the computer's number

if guess > 10 or guess < 1:

print("Please choose numbers in between 1 and 10") # Tell the user that they picked an invalid number out of the range 1-10

if guess == number:

break # Break the main game loop if the user correctly guesses the computer's number

if guess == number:

print(f"You guessed the number in {str(guesses)} tries!") # Tell the user that they guessed the computer's number in however many tries

else:

print(f"You didn't guess the number, the number was {str(number)}.") # Tell the user that they did not guess the number in 5 tries