Python Case Study-1
Python Case Study-1
STUDY
NUMBER GUESSING
GAME
INDEX:
●ABSTRACT
●INTRODUCTION
●SRS(SOFTWARE REQUIREMENT
SPECIFICATIONS)
1. Functional Requirements
2. Non-Functional Requirements
● METHODOLOGY
●RESULTS
●CONCLUSION
●FUTURE SCOPE
ABSTRACT:
The number guessing game is a fun and interactive
way to apply basic programming concepts such as
loops, conditionals, user input, and random number
generation. It challenges players to use logic and
strategy to guess the correct number within a set
range. Through this project, we demonstrated how
even a simple game can help build a strong
foundation in coding, improve problem-solving skills,
and provide an engaging user experience. This
game can also be further enhanced with features
like difficulty levels, limited attempts, hints, and score
tracking.
INTRODUCTION:
A Number Guessing Game will generate a random
number within a specified range, allow the user to
input guesses, provide feedback on whether the
guess is too high or too low or too close(If the
number guessed is close to the computer generated
number by three units to the left or right a "too close"
statement is surfaced), track the number of
attempts(The hard level has 10 attempts while the
easy level has 5 attempts) and determine the end
game conditions when the user guesses correctly or
runs out of attempts. The game should also display
guess again and congratulatory messages, and
potentially offer different difficulty levels with varying
numbers of attempts.
It is a classic and exciting game of logic, intuition,
and a little bit of luck! This game is simple yet
engaging, making it perfect for players of all ages.
The concept is straightforward: the computer
randomly selects a secret number within a
predefined range (for example, between 1 and
100), and your task is to figure out what that
number is through a series of guesses.
SRS
Here's a more detailed breakdown of the software
requirements:
Functional Requirements:
●Random Number Generation
●The software must be able to generate a
random number within a specified range (e.g.,
1-100).
●User Input:
The software should allow the user to input their
guess, which can be done through a
command-line interface (CLI) or a graphical user
interface (GUI).
●Guess Validation:
The software should validate the user's input to
ensure it's a valid number within the acceptable
range.
●Feedback:
The software should provide feedback to the
user after each guess, indicating whether the
guess is too high or too low.
●Attempt Tracking:
The software should keep track of the number of
attempts the user has made.
●End Game Conditions:
The game should end when the user guesses
the correct number or runs out of attempts.
●Win Condition:
The software should display a congratulatory
message when the user guesses the correct
number, including the number of attempts it
took.
●Loss Condition:
The software should display a message when
the user runs out of attempts.
●Optional Features:
Difficulty Levels: The game could offer different
difficulty levels that adjust the range of numbers
or the number of attempts allowed.
●Multiple Rounds: The game could allow the user
to play multiple rounds.
●High Score Tracking: The game could track and
display high scores.
Non-Functional Requirements:
●User-Friendliness:
The game should be easy to use and
understand, with clear instructions and
feedback.
●Performance:
The game should be responsive and not take
too long to execute, especially for generating
random numbers and processing user input.
●Error Handling:
The software should handle invalid input or
unexpected errors gracefully, such as by
displaying error messages.
●Maintainability:
The code should be well-structured and
documented, making it easy to maintain and
modify in the future.
●Security:
If the game involves user accounts or other
sensitive information, it should be designed to
be secure against unauthorized access.
CODE
import random
EASY_LEVEL_ATTEMPTS = 10
HARD_LEVEL_ATTEMPTS = 5
def set_difficulty(level_chosen):
if level_chosen == "easy":
return EASY_LEVEL_ATTEMPTS
elif level_chosen == "hard":
return HARD_LEVEL_ATTEMPTS
else:
print("Invalid difficulty. Defaulting to easy.")
return EASY_LEVEL_ATTEMPTS
🎉
else:
print(f" Congrats! You guessed it right. The answer was {answer}.")
return "correct"
return attempts - 1
def game():
print("I'm thinking of a number between 1 and 50.")
answer = random.randint(1, 50)
level = input("Choose your difficulty (easy or hard): ").lower()
attempts = set_difficulty(level)
guessed_number = None
while attempts > 0:
print(f"\nYou have {attempts} attempts remaining.")
try:
guessed_number = int(input("Make a guess: "))
if guessed_number < 1 or guessed_number > 50:
print("Please guess a number between 1 and 50.")
continue
except ValueError:
print("Invalid input. Please enter a number.")
continue
game()
FUTURE SCOPE:
Convert your text-based game into a graphical one using Tkinter (built-in)
or PyQt for a more professional look.
Let the computer guess your number using binary search or a simple AI
algorithm.
Store player guesses, time taken, and high scores in a local file (like JSON
or CSV).
Optionally use SQLite for a small database to keep track of scores.
Make your code cleaner and more robust by writing tests for the core logic.
Enhance your terminal game by using colorama or rich for text colors,
animations, and better visuals.
Educational Version
Add a "learn mode" that gives hints and explains logic to younger users.
Vidya (2024025667)
Anjana (2024014012)
Chavi Soni (2024003598)
Harini (2024000833)