Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 16 additions & 8 deletions Number Guessing Game/numberGuessingGame.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# coding=utf-8
import random
# random module is a built-in module to generate pseudo-random variables

Expand All @@ -19,10 +20,10 @@ def startGame():
displayGameplay = display_gameplay()
# make a list of the possible inputs
# to start or end the game
POSSIBLE_RESPONSES = ['Y','YES','N','NO','EXIT']
possible_responses = ['Y','YES','N','NO','EXIT']
# get user's response
user_response = input('\nStart game? (yes/no): ').strip().upper()
while user_response not in POSSIBLE_RESPONSES:
while user_response not in possible_responses:
print('\nInvalid Input!')
user_response = input('\nStart game? (yes/no): ').strip().upper()
else: return user_response
Expand All @@ -36,13 +37,15 @@ def game():
play_game = startGame()
# assign the number of trials the user has to a variable
number_of_trials = 7
# initialise new_game to true
new_game = True
while play_game == 'YES' or play_game == 'Y':
# make a list that contains all the
# numbers a user can guess
ACCEPTED_NUMBER_PICKS = [str(i) for i in range(1,11)]
accepted_number_picks = [str(i) for i in range(1,11)]
# get user's number
user_input = input('\nGuess a number between the range of 1-10: ').strip().upper()
while user_input not in ACCEPTED_NUMBER_PICKS and user_input != 'EXIT' :
while user_input not in accepted_number_picks and user_input != 'EXIT' :
print('Invalid Input!')
user_input = input('\nGuess a valid number between the range of 1-10: ').strip().upper()
if user_input == 'EXIT':
Expand All @@ -51,7 +54,10 @@ def game():
else:
# generate a random number in the range 1-10
# and assign it to a variable
computer_number = random.randint(1,10)
# check if new_game, if true generate new computer_number else don't
if new_game:
computer_number = random.randint(1,10)
new_game = False
user_input = int(user_input)
if user_input < computer_number:
number_of_trials -= 1
Expand All @@ -60,7 +66,7 @@ def game():
print(f'You\'ve {number_of_trials} trial(s) left')
play_game = input('\nGuess again? (yes/no): ').strip().upper()
else:
print(F'\nGame over!, you\'ve 0 trial left..try harder next time 😉')
print('\nGame over!, you\'ve 0 trial left..try harder next time 😉')
break
elif user_input > computer_number:
number_of_trials -= 1
Expand All @@ -69,14 +75,16 @@ def game():
print(f'You\'ve {number_of_trials} trial(s) left')
play_game = input('\nGuess again? (yes/no): ').strip().upper()
else:
print(F'\nGame over!, you\'ve 0 trial left..try harder next time 😉')
print('\nGame over!, you\'ve 0 trial left..try harder next time 😉')
break
elif user_input == computer_number:
number_of_trials -= 1
print(f'Congratulations!!..you guessed right, after {7 - number_of_trials} trial(s)')
play_game = input('\nDo you wish to play again? (yes/no): ').strip().upper()
# if the user wishes to play again, assign
# the number of trials the user has to a variable
number_of_trials = 7
number_of_trials = 7
# start a new game
new_game = True

game()
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,3 +81,7 @@ The contribution guidelines are as per the guide [HERE](https://github.com/larym
| 38 | [Number Guessing Game](https://github.com/Mannuel25/Python-project-Scripts/tree/main/Number%20Guessing%20Game) | [Emmanuel Tanimowo](https://github.com/Mannuel25) |
| 39 | [Web Dev with python Flask](https://github.com/deepaksaipendyala/Python-project-Scripts/tree/main/Web%20Dev%20with%20Flask) | [Deepak Sai Pendyala](https://github.com/deepaksaipendyala) |
| 40 | [password breach checker](https://github.com/deepaksaipendyala/Python-project-Scripts/tree/main/passwordbreachchecker) | [Deepak Sai Pendyala](https://github.com/deepaksaipendyala) |
| 41 | [Audio Book](https://github.com/Mannuel25/Python-project-Scripts/tree/main/AudioBuk) | [Lary Mak](https://github.com/larymak) |
| 42 | [Geocoding Google API](https://github.com/Mannuel25/Python-project-Scripts/tree/main/Geocoding%20Google%20API) | [Sherief Elsowiny](https://github.com/elsowiny) |
| 43 | [News Article Scraping](https://github.com/Mannuel25/Python-project-Scripts/tree/main/News_Article_Scraping) | [SasiKalyan](https://github.com/KanakamSasikalyan) |
| 44 | [Sudoku Solver](https://github.com/Mannuel25/Python-project-Scripts/tree/main/SudokuSolver) | [Ruben Grande Muñoz](https://github.com/RgrMz) |