Merge pull request 'The Guess the Number, object-oriented version' (#13) from Yousri/python_course_doc:master into master
Reviewed-on: https://gitea.86thumbs.net/waldek/python_course_doc/pulls/13
This commit is contained in:
commit
ebb13f83e3
|
@ -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
|
||||
---
|
||||
|
||||
|
|
Loading…
Reference in New Issue