diff --git a/learning_python3.md b/learning_python3.md index f6c10ff..44694b8 100644 --- a/learning_python3.md +++ b/learning_python3.md @@ -1311,6 +1311,63 @@ Coming up with challenges is on of the most *challenging* aspect op learning how Your thought process will send you of into unknown territory and will force you to expand you knowledge. We'll get back to this thought process later, but if you feel like an extra challenge go for it! +Below you can see the same game but broken down into functions. + +
+ Spoiler warning + +```python3 +import random + + +def ask_for_number(): + while True: + human_number = input("what is your guess? ") + if human_number.isdigit(): + human_number = int(human_number) + break + else: + print("that is not a number! please try again...") + return human_number + + +def are_the_numbers_equal(computer_number, human_number): + if human_number < computer_number: + print("my number is bigger") + return False + elif human_number > computer_number: + print("my number is smaller") + return False + elif human_number == computer_number: + print("yes! {} is the number I had in mind".format(computer_number)) + return True + + +def play_a_game(): + computer_number = random.randint(0, 100) + print("cheat mode: {}".format(computer_number)) + print("I have a number in mind...") + while True: + human_number = ask_for_number() + status = are_the_numbers_equal(computer_number, human_number) + if status == True: + break + + +def main(): + while True: + play_a_game() + play_again = input("do you want to play a new game? (Y/N)") + if play_again.startswith("N"): + print("bye bye!") + break + + +if __name__ == "__main__": + main() +``` + +
# Lists The different built-in objects we've seen until now, such as `str` and `int` are simple [text](https://docs.python.org/3/library/stdtypes.html#text-sequence-type-str) and [numeric](https://docs.python.org/3/library/stdtypes.html#numeric-types-int-float-complex) types.