Number Guessing Game using Python

Introduction

This project is a simple number guessing game implemented in Python. The program generates a random number between 1 and 100, and the player has to guess the number. The program provides feedback on whether the guessed number is too low or too high until the correct number is guessed.

Prerequisites

  • Basic understanding of Python programming.
  • Familiarity with loops, conditional statements, and user input.

Learning Objectives

  • Learn how to use the random module to generate random numbers.
  • Understand how to take user input and provide interactive feedback.
  • Develop a simple command-line game in Python.

Installation Guide

To run this project on your machine, ensure you have Python installed. Follow these steps:

  1. Download and Install Python: Visit the official Python website to download and install Python if you haven’t already.
  2. Set Up a Python Environment: Use a code editor like Visual Studio Code or an IDE like PyCharm to write and run your code.
  3. Clone or Copy the Code: Copy the project code provided below into a new Python file in your development environment.

Project Steps

  1. Import the Random Module: Use the random module to generate a random number.
  2. Generate a Random Number: Use random.randrange(1, 100) to generate a random number between 1 and 100.
  3. Get User Input: Use input() to prompt the user to guess the number.
  4. Provide Feedback: Use conditional statements to check if the guess is too low, too high, or correct, and provide appropriate feedback.
  5. Repeat Until Correct: Use a while loop to keep asking the user for a guess until the correct number is guessed.
  6.  

Code

				
					import random

n = random.randrange(1,100)
guess = int(input("Enter any number: "))
while n!= guess: 

    if guess < n:
        print("Too low")
        guess = int(input("Enter number again: "))
    elif guess > n:
        print("Too high!")
        guess = int(input("Enter number again: "))
    else:
        break
print("you guessed it right!!")
				
			

Code

				
					import random

n = random.randrange(1,100)
guess = int(input("Enter any number: "))
while n!= guess: 
    if guess < n:
        print("Too low")
        guess = int(input("Enter number again: "))
    elif guess > n:
        print("Too high!")
        guess = int(input("Enter number again: "))
    else:
        break
print("you guessed it right!!")
				
			

Pseudo Code explaining this Python Project

				
					1. Import the random module.
2. Generate a random number between 1 and 100.
3. Prompt the user to enter a guess.
4. While the guessed number is not equal to the random number:
   a. If the guessed number is less than the random number, print "Too low" and prompt for another guess.
   b. If the guessed number is greater than the random number, print "Too high" and prompt for another guess.
5. Once the correct number is guessed, print "You guessed it right!!"

				
			

Pseudo Code explaining this Python Project

				
					1. Import the random module.
2. Generate a random number between 1 and 100.
3. Prompt the user to enter a guess.
4. While the guessed number is not equal to the random number:
   a. If the guessed number is less than the random number, print "Too low" and prompt for another guess.
   b. If the guessed number is greater than the random number, print "Too high" and prompt for another guess.
5. Once the correct number is guessed, print "You guessed it right!!"

				
			

Flow Chart

Code Explanation

  • import random: This imports the random module to generate random numbers.
  • n = random.randrange(1, 100): This generates a random number between 1 and 100.
  • guess = int(input(“Enter any number: “)): This prompts the user to enter a number and converts the input to an integer.
  • while n != guess:: This loop continues until the guessed number is equal to the generated number.
  • Conditional Statements:
    • if guess < n:: Checks if the guessed number is less than the generated number and provides feedback.
    • elif guess > n:: Checks if the guessed number is greater than the generated number and provides feedback.
    • else:: Breaks the loop when the correct number is guessed.

Challenges and Solutions

  • Challenge: Handling non-integer input.
    • Solution: Use a try-except block to catch and handle exceptions when converting user input to an integer.
  • Challenge: Ending the game gracefully.
    • Solution: Add a condition to exit the loop or the program if needed, like implementing a quit option.

Testing

  • Test the program by running it and guessing different numbers.
  • Verify that the program gives correct feedback for each guess.
  • Check edge cases such as guessing the boundary numbers 1 and 100.

Additional Resources

FAQs

Q: How can I modify the range of numbers?

A: Change the arguments in random.randrange() to set a different range.

Q: How can I add a feature to count the number of guesses?

A: Initialize a counter variable and increment it each time the user makes a guess.

Q: What if the user wants to quit the game?

A: Add a condition to check if the user enters a specific value (e.g., 0) to exit the loop and end the game. Use a prompt to inform the user of this option.

Project by Nimisha Agrawal and Documented by Aakarsh Pandey, Team edSlash.