From 057531b177b0ea7912106b75744211424b87db27 Mon Sep 17 00:00:00 2001 From: waldek Date: Wed, 3 Nov 2021 11:16:24 +0100 Subject: [PATCH] adds while loop --- learning_python3.md | 150 +++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 149 insertions(+), 1 deletion(-) diff --git a/learning_python3.md b/learning_python3.md index e504957..46a7218 100644 --- a/learning_python3.md +++ b/learning_python3.md @@ -1072,7 +1072,155 @@ There are a couple of things you should definitely read up on. # While loop -TODO guess the number exercise +We started our python journey with fully linear code. +Next we saw functions which are first **defined** and called afterwards. +Now we'll have a look at **loops**. +In python there are **two** types of loops, a **while** and a **for** loop. +We'll start with the while loop which I see as a loop in *time*. +The for loop is a loop in *space* but we'll get to that one later. + +The concept of a while loop is pretty simple. +Code **within** the loop will be executed as long as a **condition** is met. +Consider the code below. + +```python3 +import time + +counter = 0 +print("before the loop, counter: {}".format(counter)) + +while counter <= 10: + print("inside the loop, counter: {}".format(counter)) + counter += 1 + time.sleep(1) + +print("after the loop, counter: {}".format(counter)) +``` + +Two *extra* things might look new to you here. +First the `import time` and `time.sleep(1)`, can you tell me what it does? +Next the `counter += 1` which is called [incrementing](https://stackoverflow.com/questions/1485841/behaviour-of-increment-and-decrement-operators-in-python). +You'll find this feature in most languages. +You can think of it's syntax as *counter equals itself plus 1*. +The *1* can be any number you want though! + +When learning the `while` [keyword](https://docs.python.org/3/reference/compound_stmts.html#the-while-statement) there is a *second* keyword you should learn. +It comes in very handy when constructing [infinite loops](https://en.wikipedia.org/wiki/Infinite_loop). +Consider the following code. + +```python3 +import time + +counter = 0 +print("before the loop, counter: {}".format(counter)) + +while True: + print("inside the loop, counter: {}".format(counter)) + counter += 1 + time.sleep(1) + +print("after the loop, counter: {}".format(counter)) +``` + +The `while True` condition is *always* `True` so the loop will **never** exit! +This is what we call an infinite loop. +The `break` keyword was added to the language so we can *break out* of a loop. +The logic is as follows. + +```python3 +import time + +counter = 0 +print("before the loop, counter: {}".format(counter)) + +while True: + print("inside the loop, counter: {}".format(counter)) + counter += 1 + if counter >= 10: + print("I'll break now!") + break + time.sleep(1) + +print("after the loop, counter: {}".format(counter)) +``` + +Infinite loops are a cornerstone of modern programming. +While they might look scary, don't overthink it, you'll get used to them very quickly. + +⛑ **When testing out an infinite loop it's sometimes handy to insert a `time.sleep` in it to slow down the execution a bit so you can wrap your head around what's happening.** + +🏃 Try it +--- + +Go back to the Celsius to Farenheit converter and add a while loop to ensure the user put's in only numbers. + + +# Coding challenge - Guess the number + +Now that you know how to repeat code execution we can create our first game! +Everybody knows the *guess the number* game. +The computer chooses a *random* number and the user has to *guess* which number it is. +At each try the computer will till you if the user's number is bigger or smaller *than* the one the computer has in mind. +The flow of the game could be as follows. + +``` +I have a number in mind... +What's your guess? 50 +my number is bigger +What's your guess? 80 +my number is smaller +What's your guess? blabla +that's not a number! try again... +What's your guess? 76 +yes, that's right! you win! +bye bye... +``` + +
+ Spoiler warning + +```python3 +import random + + +def ask_for_number(): + result = input("What's your guess? ") + if result.isdigit(): + number = int(result) + return number + else: + return None + + +if __name__ == "__main__": + number_to_guess = random.randint(0, 100) + print("I have a number in mind...") + while True: + user_number = ask_for_number() + if user_number is None: + print("that's not a number! try again...") + continue + elif number_to_guess == user_number: + print("yes, that's right! you win!") + break + elif number_to_guess > user_number: + print("my number is bigger") + elif number_to_guess < user_number: + print("my number is smaller") + print("bye bye...") +``` + +
+ +🏃 Try it +--- + +My *solution* is very basic. +Think of some ways to improve on it. +Can you limit the number of tries? +Can you add a feature to let the user play a *second* game after he/she wins or loses? +Coming up with challenges is on of the most *challenging* aspect op learning how to program. +Your thought process will send you of into unknown territory and will force you to expand you knowledge! # Lists