Skip to content

husseini2000/Number-Gussing-Game

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

5 Commits
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

๐ŸŽฏ Number Guessing Game (Python)

A simple yet polished Number Guessing Game written in Python.
This game demonstrates clean coding practices, Pythonic refactoring, type hinting, and structured documentation using PEP 8 and PEP 257 standards.


๐Ÿงฉ Project Overview

The player tries to guess a randomly chosen number within a user-defined range.
The program provides hints, tracks the number of attempts, and offers an optional โ€œcheat modeโ€ for debugging or fun testing.

This project was refactored to:

  • Replace manual loops with Python built-ins (next(), join(), enumerate()).
  • Add comprehensive docstrings for all functions.
  • Include type hints for clarity and IDE/static analysis support.
  • Follow PEP 8 (style) and PEP 257 (docstring) guidelines.
  • Improve game structure and flow for readability and reusability.

โš™๏ธ Features

โœ… User-defined range for random number
โœ… Cheat input (-1) reveals the secret number
โœ… Hint system for higher/lower and โ€œwithin 1โ€ feedback
โœ… Multiple attempts (default 4)
โœ… Clean game summary for each round
โœ… Play again option
โœ… Fully type-annotated, well-documented code


๐Ÿš€ How to Run

  1. Clone the repository:

    git clone https://github.com/husseini2000/Number-Gussing-Game.git
    cd number-guess-game
  2. Run the game:

    python number_guess.py

๐Ÿง  Game Flow

  1. Youโ€™ll be asked to enter a lowest and highest number.
  2. The computer randomly selects a number between them.
  3. Youโ€™ll have 4 attempts to guess it.
  4. If your guess is close (within 1), youโ€™ll get a hint.
  5. Enter -1 to reveal the answer.
  6. After the round, you can choose to play again.

๐Ÿ“š Code Structure

number_guess.py
โ”œโ”€โ”€ wel_message()          # Display welcome banner
โ”œโ”€โ”€ number_to_guess()      # Set range and generate random number
โ”œโ”€โ”€ get_guess()            # Collect user guesses safely
โ”œโ”€โ”€ handle_cheat_input()   # Reveal number if user enters -1
โ”œโ”€โ”€ give_feedback()        # Provide intelligent feedback
โ”œโ”€โ”€ play_round()           # Control game logic per round
โ””โ”€โ”€ play_game()            # Run overall game loop

๐Ÿงฉ Problems Identified (Before Refactoring)

Issue Description
๐Ÿ” Repetitive loops Used while loops for checks that can be replaced by built-ins (next, join, any).
โš™๏ธ Hardcoded retry logic Maximum attempts (4) were fixed without easy modification.
๐ŸŽฏ Random number persistence The same random number was reused across new rounds.
๐Ÿงพ No type hints or docstrings Functions lacked clear definitions and expected input/output documentation.
๐Ÿงฉ Confusing cheat logic The โ€œcheat modeโ€ function name didnโ€™t reflect its simple purpose.
๐Ÿšจ Weak error handling Non-integer input could crash the program.
๐Ÿงน Unclear flow Replay and attempt tracking were spread across multiple functions, reducing readability.

๐Ÿ’ก Improvements Implemented

Category Improvement
๐Ÿง  Readability All functions now have PEP 257โ€“compliant docstrings and clear names.
๐Ÿ” Maintainability Type hints added to every function and variable where applicable.
๐Ÿงฐ Pythonic Style Replaced manual loops with next(), join(), and comprehensions.
๐Ÿงฉ Error Handling Input validation ensures non-integer inputs are handled gracefully.
๐ŸŽฎ User Experience Added feedback messages, summary of guesses, and emojis for engagement.
๐Ÿงฑ Extensibility Structure supports future features (e.g., difficulty levels, scoring).

๐Ÿงฐ Tech Stack

  • Language: Python 3.10+
  • Libraries: Built-in (random, typing)
  • Standards: PEP 8 (style), PEP 257 (docstrings), Type Hints (PEP 484)

๐Ÿงช Future Improvements

  • ๐Ÿงฉ Difficulty Levels: Easy / Medium / Hard (dynamic range and attempts)
  • ๐Ÿ† Score Tracking: Maintain win/loss stats across sessions
  • ๐Ÿ’พ Logging: Replace prints with Pythonโ€™s logging for better debugging
  • ๐Ÿ” Unit Testing: Add pytest tests for each function
  • ๐Ÿ’ป CLI / GUI Version: Expand into a command-line or graphical version using Tkinter or PySimpleGUI

๐Ÿง‘โ€๐Ÿ’ป Author

Al-Husseini Ahmed Abdelaleem

  • ๐Ÿ“ง Gmail
  • ๐ŸŒ LinkedIn

๐Ÿชช License

This project is open-source and available under the MIT License.


๐Ÿง  Summary

This project demonstrates how a simple Python program can evolve into a clean, professional, and maintainable codebase through refactoring and adherence to best practices.

โ€œClean code always looks like it was written by someone who cares.โ€ โ€“ Robert C. Martin

About

๐ŸŽฏ Number Guessing Game โ€” Clean Code Edition ๐Ÿ A modern, console-based Python game built with clean code principles. This project demonstrates real-time feedback, difficulty levels, cheat mode, and type-hinted, well-documented functions following PEP 8 & PEP 257 standards.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages