From 62ddfad1a6403417074dcde13351730e47dddec4 Mon Sep 17 00:00:00 2001 From: Yousri <54667@etu.he2b.be> Date: Thu, 12 May 2022 11:08:50 +0200 Subject: [PATCH] The Guess the Number, object-oriented version --- learning_python3.md | 63 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) diff --git a/learning_python3.md b/learning_python3.md index df82722..47d5281 100644 --- a/learning_python3.md +++ b/learning_python3.md @@ -1322,6 +1322,69 @@ if __name__ == "__main__": +
+ + The Object Oriented program of Guess The Number + +```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. +
+ 🏃 Try it ---