Lesson 3 Discovering your Paths Chapter 1: Introduction Chapter 2: Comparison operators Chapter 3: If statements [3.1] If statement [3.2] If you try, you will learn [3.3] Python indentation [3.4] Multi-way selection [3.5] Test your understanding Chapter 4: Applying your knowledge Chapter 5: Making a git commit Chapter 6: Function calls Chapter 7: Using existing functions Chapter 8: Composition and abstraction Chapter 9: Incremental development Chapter 10: Commenting Chapter 11: Summary Chapter 3: If statements If you try, you will learn face Josiah Wang Now, let us test whether you really understand using if/if-else statements! Try to predict the output for the code snippets below. 1 2 3 4 5 ❮ ❯ Question 1 What does the following code snippet output? Type Blank if the code does not output anything, and Error if it produces an error. if 8 < 5: print("Yes") Output Blank Check answer! Question 2 What does the following code snippet output? Type Blank if the code does not output anything, and Error if it produces an error. if 8 < 5: print("Yes") else: print("No") Output No Check answer! Question 3 What does the following code snippet output? Type Blank if the code does not output anything, and Error if it produces an error. age = 50 if age <= 50: print("Young") else: print("Wise") Output Young Check answer! Question 4 What does the following code snippet output? Type Blank if the code does not output anything, and Error if it produces an error. coins = 5 if coins < 2: coins = coins + 2 print(coins) Bonus activity: Can you explain (in plain English) what this code is trying to do? Try explaining it to a rubber duck or equivalent. Output 5 Explanation: This piece of code adds 2 coins to your coins if you have fewer than 2 coins. Otherwise it does nothing. It will print the number of coins you have at the end of the program. Check answer! Question 5 What does the following code snippet output? Type Blank if the code does not output anything, and Error if it produces an error. if 5: print("Yes") else: print("No") Output Yes Explanation: This piece of code actually works. It is hard to read though, so please do NOT write such codes. If a condition is not a boolean expression, you can assume that the value of any object is True by default, unless the value is similar to 0 or "empty". For example, if 0: or if "": will evaluate to False. You can check whether an object will evaluate to True or False by using bool(). Try bool(5), bool(-1), bool("love"), bool(""), bool(0), bool(0.0) in an interactive prompt. This is not important for the course (since I am discouraging you to write like this). But it might be useful to know when you are debugging your code for example! You can read this article if you are interested in understanding this further. Check answer!