IF and WHILE loop
Most Basic loops in python
Homework Answers from last time (ignore if you didn't read last lesson)
Last time I gave ya the task to make a function that will print whatever you will input [by input I mean user input]

Remember that you can name the function however you want and its paramater and the message from input can also be changed to anything.
Your try may looked like this :

This is basicly the same thing , but you save the input to a variable then make use of that variable . The way I did it on the first screenshot was the optimal way since the input result was gonna be used once.
IF , the most basic conditional statement
What it is and how useful it is
The most simplified version of this loop is : IF will run a code block only if a certain condition is met
Without this type of loop a LOT of tasks will become impossible . What IF [unintended pun] I give you the task to make a function that will print what you give it , but if the input is 5 then it should print twice the input.
To finish that task you need a way to check if the input given by user is the same as 5.
The syntax of IF is the following
If condition:
<codeblock>

Before we will continue our main task , we will talk about how the IF works more in-depth
How does it actually work?
In the example up there there is example == 5 , basicly it checks if the variable called 'example' if its equal to 5 , if its equal then it will simplify and transform into a boolean accordingly [True if the example == 5 , and False otherwise]

I hear you ask , what if I do if 5: , will it even run , is 5 true or false? The answer is that it will run , because if the condition result is not a boolean it will convert into one.
Also if you try to make a string into boolean it will be True in every case except when the string is completly empty .
Finishing the task
Let's make first a function that will print everything we input [Look at the first screenshot on this specific page]
You can do it in a lot of ways , but I'll go for a main IF that will also contain an ELSE which is pretty easy to get.
We will check if the variable is equal to 5 , If yes then we print it twice , if no then we print it only once

And that's about the IF
WHILE loops
How are they different from IF ones?
The IF function will check on a condition and run the code only once . But what if you wanna check it 2 times , because the input may be different.
If i will do something like if variable < 5: variable = variable + 1 , this will increment the variable ONCE if its smaller than 5 and it will increment twice if you put the IF twice and have the condition [ variable < 5] met.
Well while loops are made for this case , not this specific case , but for a lot of cases when you don't wanna put 20 IF statements in case your number needs to be higher than 5 but not higher than 5 without redefining the variable
To use a while loop just do while(condition): <block> and it will basicly repeat 'till the condition is False.

In this example , the output will be 'message' but 4 times on different lines and it wil be 'done' after those lines.
Why does it work like that?
I can show you how in a really easy way by modifying the code up there

This code is the exact same thing as a while loop, but if the variable is too small it will fail to make it up to 5 . While is basicly checking the condition each time the code block ends and if its still True it will again if it's not False.
Why is this useful?
Imagine that I ask you to print("Arstotzka makes good tutorials for beginners") , but ONE HUNDRED TIMES. Well unless you are ready to spamm CTRL+D and fill an entire screen with the same message , then you should probably use a while loop.
This time instead of giving you how to do it , I'll let this be your homework and figure out yourself from the earlier examples on how to do it
Practice always makes perfect
The homework this time will be made out of 2 parts , the task up there and another task I'll give ya.
Make a function that will get user input and will print "That's true" ONLY if the user said "Glory To Arstotzka" And for every other case when user doesn't say the message , make it print("Do you mean, Glory To Arstotzka?") . On the next lesson we are gonna talk about the answers and another 2 topics.
Hey , you've got far on this tutorial. Keep up the good work! [ I'll answer python-related questions on discord]
Last updated
Was this helpful?