├── LICENSE ├── Number Guessing Game.py ├── README.md └── step-by- step - explanation.pdf /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2024 Abbireddy Venkata Chandu 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /Number Guessing Game.py: -------------------------------------------------------------------------------- 1 | # number-guessing-game.py 2 | 3 | import random 4 | 5 | def main(): 6 | # Initialize counters for high, low, and win 7 | high = 0 8 | low = 0 9 | win = 0 10 | 11 | # Generate a random number between 1 and 100 12 | number = random.randint(1, 100) 13 | 14 | while win == 0: 15 | # Ask the user to input a guess 16 | userNum = int(input("Please guess a number between 1 and 100: ")) 17 | 18 | # Check if the user's guess is too high, correct, or too low 19 | if userNum > number: 20 | message = "Too high, try again." 21 | high += 1 22 | elif userNum == number: 23 | message = "You got it correct! Congratulations!" 24 | win += 1 25 | else: 26 | message = "Too low, try again." 27 | low += 1 28 | 29 | # Print the appropriate message 30 | print() 31 | print(message) 32 | 33 | # Display the total number of guesses 34 | print() 35 | print("Number of times too high:", high) 36 | print("Number of times too low:", low) 37 | print("Total number of guesses:", high + low + win) 38 | 39 | # Call the main function to start the game 40 | main() -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## Number Guessing Game in Python 2 | 3 | ### Project Objectives 4 | 5 | This project aims to: 6 | 7 | * Implement a classic number guessing game in Python. 8 | * Practice fundamental programming concepts like random number generation, user input, conditional statements, and loops. 9 | * Create a user-friendly and interactive experience. 10 | 11 | ### What I Learned 12 | 13 | Developing this game helped me solidify my understanding of: 14 | 15 | * The `random` module for generating random numbers. 16 | * Working with user input and converting it to the desired data type (integer in this case). 17 | * Utilizing conditional statements (`if`, `elif`, `else`) for decision making. 18 | * Implementing loops (`while`) for repetitive tasks. 19 | * Displaying formatted output using `print` statements. 20 | 21 | ### Project Purpose 22 | 23 | This number guessing game serves as a: 24 | 25 | * **Learning tool:** It reinforces core Python programming concepts in a practical and engaging way. 26 | * **Simple entertainment:** It provides a casual game experience for users to test their luck and intuition. 27 | 28 | ### How it Works 29 | 30 | 1. The program imports the `random` module. 31 | 2. It defines a function `main()` to encapsulate the game logic. 32 | 3. Inside `main()`: 33 | * Counters are initialized for high guesses, low guesses, and wins. 34 | * A random number between 1 and 100 is generated and stored. 35 | * A `while` loop continues until the player guesses correctly. 36 | * Inside the loop, the user is prompted to enter a guess. 37 | * The guess is compared to the secret number. 38 | * Based on the comparison (too high, too low, or correct), appropriate messages are displayed, and counters are updated. 39 | * After the loop, the total number of high, low, and overall guesses is displayed. 40 | 4. The `main` function is called to start the game. 41 | 42 | ### Efficiency 43 | 44 | This implementation prioritizes readability and understanding over optimization. While the code is efficient for a simple game, more complex projects might require techniques to improve performance for larger data sets or frequent calculations. 45 | 46 | ### Further Development 47 | 48 | * Difficulty levels with adjustable number ranges. 49 | * Limited number of attempts to add an element of challenge. 50 | * Score tracking based on the number of guesses. 51 | 52 | -------------------------------------------------------------------------------- /step-by- step - explanation.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/venkat-0706/Number-Guessing-Game---Python/82891496abbcc8e4968b3ad24b68aaf7365a67ce/step-by- step - explanation.pdf --------------------------------------------------------------------------------