Skip to content

Commit 11382f7

Browse files
authored
Merge pull request #2434 from Shivansh-Jain-github/cont1
Adding script, README.md file for Guess the number game
2 parents 08b7bdd + a83da9d commit 11382f7

File tree

2 files changed

+79
-0
lines changed

2 files changed

+79
-0
lines changed

Guess the number/README.md

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# Guess the Number Game - README
2+
3+
## Introduction
4+
5+
Welcome to the "Guess the Number" game! This Python script allows you to play a simple guessing game where you have to guess a randomly chosen number between 1 and 100. The game will provide you with feedback for each guess you make, helping you determine whether your guess is too low or too high.
6+
7+
## How to Play
8+
9+
1. **Launch the Game**: Run the Python script (guess_the_number.py) in your preferred Python environment.
10+
2. **Game Rules**: The computer will randomly select a secret number between 1 and 100, which you have to guess.
11+
3. **Making a Guess**: You will be prompted to enter your guess. Input an integer between 1 and 100 and press Enter.
12+
4. **Feedback**: After each guess, the game will provide feedback to help you adjust your next guess:
13+
- If your guess is correct, you will receive a congratulatory message along with the number of attempts it took you to guess correctly.
14+
- If your guess is too low, the game will inform you to try a higher number.
15+
- If your guess is too high, the game will suggest trying a lower number.
16+
5. **Invalid Input Handling**: If you input anything other than a valid integer, the game will notify you and prompt for a new guess.
17+
6. **Keep Guessing**: Continue making guesses until you correctly guess the secret number.
18+
19+
## Example
20+
21+
```
22+
Welcome to Guess the Number Game!
23+
I'm thinking of a number between 1 and 100.
24+
Enter your guess: 50
25+
Too low! Try again.
26+
Enter your guess: 75
27+
Too high! Try again.
28+
Enter your guess: 65
29+
Too high! Try again.
30+
Enter your guess: 60
31+
Too low! Try again.
32+
Enter your guess: 62
33+
Congratulations! You guessed the number in 5 attempts.
34+
```
35+
36+
## Requirements
37+
38+
This game is written in Python and requires no external libraries. You can run it using any Python 3.x interpreter.
39+
40+
## Contribution
41+
42+
If you have any ideas or suggestions to enhance the game or improve the code, feel free to contribute! Fork the repository, make your changes, and create a pull request.
43+
44+
## License
45+
46+
This project is licensed under the [MIT License](LICENSE).
47+
48+
## Contact
49+
50+
For any questions or feedback, please contact:
51+
52+
Shivansh Jain
53+

Guess the number/script.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import random
2+
3+
def guess_the_number():
4+
print("Welcome to Guess the Number Game!")
5+
print("I'm thinking of a number between 1 and 100.")
6+
7+
secret_number = random.randint(1, 100)
8+
attempts = 0
9+
10+
while True:
11+
try:
12+
guess = int(input("Enter your guess: "))
13+
attempts += 1
14+
15+
if guess == secret_number:
16+
print(f"Congratulations! You guessed the number in {attempts} attempts.")
17+
break
18+
elif guess < secret_number:
19+
print("Too low! Try again.")
20+
else:
21+
print("Too high! Try again.")
22+
except ValueError:
23+
print("Invalid input. Please enter a valid number.")
24+
25+
if __name__ == "__main__":
26+
guess_the_number()

0 commit comments

Comments
 (0)