In this post, we will see how to make a number guessing game in python.
So, let’s talk about the game, the user guesses a number in the fixed range then the program tells it is a correct guess or not. If not then the user can guess again.
Now, we have to do the following Tasks in this game:
- Asks User to enter a range i.e. the value of low and high.
- Automatically randomly select a number in the given range.
- Asks user to guess a number in the given range.
- Checks if the number entered by the user is correct guess or not.
- Provides a hint to the user whether the number entered by him is lower or higher than the actual number.
- keeps asking the user for a number until he guesses the correct number or accepts his defeat.
Example:
Enter lower limit: 10
Enter higher limit: 20
Guess any number in the given range 10 to 20
16
You Lost
Lesser value guess
Do you want to guess again: y/n
y
Guess any number in the given range 10 to 20
18
You Lost
Lesser value guess
Do you want to guess again: y/n
y
Guess any number in the given range 10 to 20
19
You Win
Let’s see the implementation :
# import random library import random # define guess function # for guessing a number def guess(): # str function convert integer # value into string value print("Guess any number in the given range", lower, "to", higher) # input() takes input in # the form of string and # int function convert # string value into integer guess_user = int(input()) # keep looping until guess_user # not in given range and asking # for new input while guess_user not in range(lower, higher + 1): print("Invalid guess \n Please guess again: ") guess_user = int(input()) # return the value of # guess_user from where # it is callinhg return guess_user # main code if __name__ == "__main__" : # taking integer input from user lower = int( input("Enter lower limit: ")) higher = int( input("Enter higher limit: ")) # random.randint() means we # are using randint() function # of random library. # randint(lower,higher) gives # random value in between lower # and higher value including # thesen two guess_computer = random.randint(lower, higher) # calling guess function guess_user1 = guess() # keep looping until user won or # accepted his defeat while True: # if this condition is true then # break out of the loop if guess_user1 == guess_computer : print("You Win ") break # if condition is false then this # block of code is executed else: print("You Lost ") if guess_user1 > guess_computer : print("Greater value guess ") else: print("Lesser value guess ") choice = input("Do you want to guess again: y/n \n ") # if this condition is true # then break out of while loop if (choice == 'n' or choice == 'N'): print("So sad.. \n You accepted your Defeat..") break # other wise again guess function # is called for user guessing else: guess_user1 = guess()
Output:
Enter lower limit: 10 Enter higher limit: 20 Guess any number in the given range 10 to 20 16 You Lost Lesser value guess Do you want to guess again: y/n y Guess any number in the given range 10 to 20 18 You Lost Lesser value guess Do you want to guess again: y/n y Guess any number in the given range 10 to 20 19 You Win