Skip to content

Commit 6fd020f

Browse files
authored
Merge pull request larymak#59 from Mannuel25/main
Addition of Number Guessing Game
2 parents f21926d + edee4b4 commit 6fd020f

File tree

4 files changed

+105
-0
lines changed

4 files changed

+105
-0
lines changed

Number Guessing Game/README.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Number Guessing Game
2+
3+
This is a game that allows you guess a number between the range of 1- 10 in just 7 trials.
4+
5+
# Prerequisites
6+
7+
It requires no prerequisites, you only need to run the script. If you don't have Python installed, you can visit [here](https://www.python.org/downloads/) to download Python
8+
9+
# How to run the script
10+
11+
Running the script is pretty easy, open a terminal in the folder where your script is located and run the following command :
12+
13+
`python numberGuessingGame.py`
14+
15+
# Sample use of the script
16+
17+
![alt text](https://github.com/Mannuel25/Python-project-Scripts/blob/main/Number%20Guessing%20Game/script_screenshot.png)
18+
19+
20+
# Author's name
21+
22+
[Emmanuel Tanimowo](https://github.com/Mannuel25)
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
import random
2+
# random module is a built-in module to generate pseudo-random variables
3+
4+
def display_gameplay():
5+
"""
6+
Displays the gameplay
7+
: return: None
8+
"""
9+
print('\nWelcome to Number Guessing Game!')
10+
print('In this game you\'ve just 7 trials to guess a number between the range of 1-10')
11+
print('Note: enter \'exit\' to end game')
12+
13+
def startGame():
14+
"""
15+
Gets user response to start or end the game
16+
: return: str
17+
"""
18+
# call the function to display gameplay
19+
displayGameplay = display_gameplay()
20+
# make a list of the possible inputs
21+
# to start or end the game
22+
POSSIBLE_RESPONSES = ['Y','YES','N','NO','EXIT']
23+
# get user's response
24+
user_response = input('\nStart game? (yes/no): ').strip().upper()
25+
while user_response not in POSSIBLE_RESPONSES:
26+
print('\nInvalid Input!')
27+
user_response = input('\nStart game? (yes/no): ').strip().upper()
28+
else: return user_response
29+
30+
def game():
31+
"""
32+
Controls the game
33+
: return: None
34+
"""
35+
# call the function to get user's response
36+
play_game = startGame()
37+
# assign the number of trials the user has to a variable
38+
number_of_trials = 7
39+
while play_game == 'YES' or play_game == 'Y':
40+
# make a list that contains all the
41+
# numbers a user can guess
42+
ACCEPTED_NUMBER_PICKS = [str(i) for i in range(1,11)]
43+
# get user's number
44+
user_input = input('\nGuess a number between the range of 1-10: ').strip().upper()
45+
while user_input not in ACCEPTED_NUMBER_PICKS and user_input != 'EXIT' :
46+
print('Invalid Input!')
47+
user_input = input('\nGuess a valid number between the range of 1-10: ').strip().upper()
48+
if user_input == 'EXIT':
49+
print('Bye Player!')
50+
break
51+
else:
52+
# generate a random number in the range 1-10
53+
# and assign it to a variable
54+
computer_number = random.randint(1,10)
55+
user_input = int(user_input)
56+
if user_input < computer_number:
57+
number_of_trials -= 1
58+
print(f'Oops, {user_input} is too low')
59+
if number_of_trials != 0:
60+
print(f'You\'ve {number_of_trials} trial(s) left')
61+
play_game = input('\nGuess again? (yes/no): ').strip().upper()
62+
else:
63+
print(F'\nGame over!, you\'ve 0 trial left..try harder next time 😉')
64+
break
65+
elif user_input > computer_number:
66+
number_of_trials -= 1
67+
print(f'Oops, {user_input} is too high')
68+
if number_of_trials != 0:
69+
print(f'You\'ve {number_of_trials} trial(s) left')
70+
play_game = input('\nGuess again? (yes/no): ').strip().upper()
71+
else:
72+
print(F'\nGame over!, you\'ve 0 trial left..try harder next time 😉')
73+
break
74+
elif user_input == computer_number:
75+
number_of_trials -= 1
76+
print(f'Congratulations!!..you guessed right, after {7 - number_of_trials} trial(s)')
77+
play_game = input('\nDo you wish to play again? (yes/no): ').strip().upper()
78+
# if the user wishes to play again, assign
79+
# the number of trials the user has to a variable
80+
number_of_trials = 7
81+
82+
game()
69.2 KB
Loading

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,3 +78,4 @@ The contribution guidelines are as per the guide [HERE](https://github.com/larym
7878
| 35 | [Image to ASCII](https://github.com/larymak/Python-project-Scripts/tree/main/image-ascii) | [Lary Mak](https://github.com/larymak) |
7979
| 36 | [Password Validator](https://github.com/larymak/Python-project-Scripts/tree/main/password-validator) | [Emmanuel Tanimowo](https://github.com/Mannuel25) |
8080
| 37 | [Text to Audio](https://github.com/larymak/Python-project-Scripts/tree/main/texttoaudio) | [Azmine Toushik](https://github.com/azminewasi) |
81+
| 38 | [Number Guessing Game](https://github.com/Mannuel25/Python-project-Scripts/tree/main/Number%20Guessing%20Game) | [Emmanuel Tanimowo](https://github.com/Mannuel25) |

0 commit comments

Comments
 (0)