-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #95 from LudovicoSforza/main
added guess a number
- Loading branch information
Showing
2 changed files
with
50 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import random | ||
|
||
|
||
class NumberGuessingGame: | ||
|
||
def __init__(self): | ||
# define the range | ||
self.LOWER = 1 | ||
self.HIGHER = 100 | ||
|
||
# method to generate the random number | ||
def get_random_number(self): | ||
return random.randint(self.LOWER, self.HIGHER) | ||
|
||
# game start method | ||
def start(self): | ||
# generating the random number | ||
random_number = self.get_random_number() | ||
|
||
print( | ||
f"Guess the randomly generated number from {self.LOWER} to {self.HIGHER}") | ||
|
||
# heart of the game | ||
chances = 0 | ||
while True: | ||
user_number = int(input("Enter the guessed number: ")) | ||
if user_number == random_number: | ||
print( | ||
f"-> Hurray! You got it in {chances + 1} step{'s' if chances > 1 else ''}!") | ||
break | ||
elif user_number < random_number: | ||
print("-> Your number is less than the random number") | ||
else: | ||
print("-> Your number is greater than the random number") | ||
chances += 1 | ||
|
||
|
||
# instantiating and starting the game | ||
numberGuessingGame = NumberGuessingGame() | ||
numberGuessingGame.start() |