#here is code in Python
# import library
import random
#function
def numberGuessingGame():
# generate random number between 1-10
num = random.randint(1,10)
no_of_g = 3
#loop to check the guess
for i in range(no_of_g):
#read inout number from user
inp_num = int(input("Guess a number between 1-10:"))
#check input is less than random number
if inp_num < num:
print("Your guess is too low")
print("", 2-i, "chances left.")
continue
#check input is greater than random number
elif inp_num > num:
print("Your guess is too high")
print("", 2-i, "chances left.")
continue
#check input is Equal random number
else:
print("You won!")
print("The number is", num)
break
#if chances are Equal to 0
if not (num == inp_num):
print("You lost!")
print("The number is", num)
#call the function
numberGuessingGame()
Explanation:
Generate a random number between 1-10.Then ask user to enter a number.If user's input is greater than random number then print "too high".If number is less than random number then print "too low".If user's input is equal to random number then print "You won!" and random number in new line. This will repeat for 3 time.If user is not able to guess the random number for 3 times then it will print "You lost!" and the random number.
Output:
Guess a number between 1-10:5
too high
2 chances left.
Guess a number between 1-10:6
too high
1 chances left.
Guess a number between 1-10:1
too low
0 chances left.
You lost!
The number is 3