The Guess the Number, object-oriented version

This commit is contained in:
Yousri 2022-05-12 11:08:50 +02:00
parent 598d499356
commit 62ddfad1a6
1 changed files with 63 additions and 0 deletions

View File

@ -1322,6 +1322,69 @@ if __name__ == "__main__":
</details>
<details>
<summary> The Object Oriented program of Guess The Number </summary>
```python
import random
class ChoiceNumberInTheRange(object):
def ReturnRandomNumber(self, range):
return random.randint(0,int(range))
class GetTheNumberOfPlayer(object):
def GetNumber(self):
try:
Number = int(input("What is the number in your mind ?"))
return Number
except:
print("I need number !!")
class Right(object):
def Verification(self, right_number, number_guessed):
if right_number == number_guessed:
SayCorrect()
exit()
else:
NotCorrect(right_number, number_guessed)
class NotCorrect(object):
def __init__(self, right_number, number_guessed):
if right_number < number_guessed:
SayBigger()
elif right_number > number_guessed :
SaySmaller()
class SayBigger(object):
def __init__(self):
print("Your number is bigger")
class SaySmaller(object):
def __init__(self):
print("Your number is smaller")
class SayCorrect(object):
def __init__(self):
print("You win")
if __name__ == "__main__":
Range = input("What is the range do you want")
RandomValue = ChoiceNumberInTheRange().ReturnRandomNumber(Range)
while True:
TheGuestResponse = GetTheNumberOfPlayer().GetNumber()
Right().Verification(right_number= RandomValue, number_guessed= TheGuestResponse)
```
All classes have a different task.
As you can see, there are classes for getting a random value in a range value (_ChoiceNumberInTheRange_ class).
Class for checking if the number is right or not (_Right_ class).
Classes for talking with the player if he wins or not.
If player getting the right value, you can use exit() method to stop the program.
</details>
🏃 Try it
---