Step 4 - Loop Statements [If-Else Loop]
- Coding GP Team Project
- Oct 14, 2020
- 1 min read
Now we will be exploring loop statement. There are many types of loops, however, we shall only focus on if-else loops. They are quite easy to implement and are extremely useful.
An If-else loop has the same logic as Boolean. If a certain condition is True, then perform A, else perform B. Here is an example:

gender = input("What is your dog's gender?: ")
if gender.lower() == "male":
print("Good boy!")
elif gender.lower() == "female":
print("Good girl!")
else:
print("What gender is that?!")Note a few things:
• The operator "==" means compared to, and in this case, you are comparing the gender variable to the string "male".
• There is a colon (":") at the end of the else and if. This is the syntax of Python.
• Every line in the if-else statement is indented. Press the button "TAB" on your keyboard, or enter 4 spaces, to create an indent.
• "elif" is a continuation of the if statement, to check for another parameter.
• Since variables are case-sensitive, we use the function .lower() to convert all strings to lower case regardless of their type. For example, MALE will convert to male.
Let's try it out!
Click here to move on to the final step!




Comments