Python Project: Number Guessing Game

Project Requirement

  • Build a Number guessing game.
  • User is asked to provide a start and end range for the game.
  • System will generate a random number from the provided range.
  • User has to guess that number in the minimum number of guesses.
  • Exit the game if user enters ‘exit’.

Additional Features:

  • Display Rules of the game.
  • Show the countdown timer before starting the game.
  • Show warning if the guessed number is outside the provided range.
  • Clear the screen 5 seconds after the game is over.
  • Maximum Guesses: 10.

Python Code

Disclaimer: This code is provided for educational purposes only. Please test the code thoroughly before using it in production. You are responsible for any issues that arise from using this code in your environment.

# Project: Number Guessing Game

# Requirement:
# Build a Number guessing game, in which the user selects a range.
# Let’s say User selected a range, i.e., from A to B, where A and B belong to Integer.
# Some random integer will be selected by the system and the user has to guess that integer in the minimum number of guesses.
# Exit the game if user enters 'exit'

import random # import random
import time # import time to add time delay

# function to clean the screen after x seconds
def cleaner(sec):
  print("Cleaning screen in",sec,"seconds.")
  time.sleep(sec)
  from IPython.display import clear_output
  clear_output()

# Print Game name and rules of the game
print("********** Welcome to the NUMBER GUESSING GAME **********")
print("Rules of the game\n")
print("* Enter a start and end range\n* Enter the number between the range\n* Maximum 10 guesses allowed\n* Enter 'exit' any time to stop the game")
print("*******************************")


# User input to provide a range

start=int(input("\nEnter the START of range :"))
stop=int(input("Enter the END of range :"))
print("\nRange for this Guessing game is..","\033[1m"+str(start),"to "+str(stop),"\033[0m\n")
print("*******************************")

# Generate the random # based on user provided range
num=random.randrange(start,stop)
#print(num)

# Countdown timer to start the game
print("Game starts in..")
for cntr in range(3,0,-1):
  time.sleep(0.5) # sleep for .5 seconds
  print(cntr)
  cntr += 1

print("\n\033[1mStart Guessing\033[0m")

#Loop to check the guesses
for i in range (1,12): # run the loop max 10 times
  if i==11: # check if out of attempts
      print("\n\033[1mOut of attempts. Better luck next time.\033[m")
      break
  else:
    user_input=str(input("\nGuess #"+str(i)+":"))
    if user_input == "exit": # exit the game if user types 'exit'
        print("\033[1mBye. Thanks for playing.\033[0m")
        break
    elif int(user_input) < start or int(user_input) > stop: #check if user enter any no. out of the provided range
      print("Guessed number is out of range",start,"-",stop)
    elif int(user_input) > num:
      print("   Nope. Guess Lower")
    elif int(user_input) < num:
      print("   Nope. Guess Higher")
    elif int(user_input) == num:
      print("   Congratulations. You're right. You guessed in\033[1m",i,"\033[0m"+"attempts.") ## Bold the text - start of bold - \033[1m , end of bold \033[0m
      break
    else:
        i +=1
        continue

print("\n")
#Call function cleaner to clear output screen after 3 seconds
cleaner(5)

Code Explanation

Explanation of code elements.

Libraries / Built -in

  • import time : Used to add time delay
    time.sleep(seconds)
  • import random : Generate random number
    random.randrange(range_start,range_stop)
  • Clear Output:
    from IPython.display import clear_output
    clear_output ()

Bolding output
\033[1m – Start of Bold
\033[0m – End of Bold

Output: Number Guessing Game

********** Welcome to the NUMBER GUESSING GAME **********
Rules of the game

* Enter a start and end range
* Enter the number between the range
* Maximum 10 guesses allowed
* Enter 'exit' any time to stop the game
*******************************

Enter the START of range :1
Enter the END of range :10

Range for this Guessing game is.. 1 to 10

*******************************
Game starts in..
3
2
1

Start Guessing

Guess #1:3
Nope. Guess Higher

Guess #2:9
Nope. Guess Lower

Guess #3:6
Congratulations. You're right. You guessed in 3 attempts.

Cleaning screen in 5 seconds.

Posted

in

by

Tags: