๐Ÿ”„ Chapter 4: Making Decisions (if/else)

Test your understanding of Python's decision-making power!

๐ŸŽฏ Multiple Choice Questions

1. What is a condition in Python programming?

  • A statement that can only be True or False
  • A type of variable
  • A function that prints text
  • A way to create loops
Correct! A condition is a statement that evaluates to either True or False, like asking a yes/no question that helps your program make decisions.

2. What happens if you forget to indent the code inside an if statement?

  • The code runs normally
  • Python gives an IndentationError
  • The code runs twice
  • Python ignores the if statement
Correct! Python requires proper indentation to know which lines belong to the if statement. Without it, you'll get an IndentationError.

3. What's the difference between = and == in Python?

  • There is no difference
  • = is for comparing, == is for assigning
  • = is for assigning, == is for comparing
  • Both are used for math operations
Correct! = assigns a value to a variable ("make this equal to that"), while == compares two values ("are these equal?").

4. What does the modulo operator (%) do?

  • Calculates percentages
  • Returns the remainder after division
  • Multiplies two numbers
  • Divides two numbers
Correct! The modulo operator (%) returns the remainder after division. For example, 13 % 5 = 3 because 13 รท 5 = 2 with remainder 3.

5. When does an else block execute?

  • Always, regardless of the if condition
  • Only when the if condition is True
  • Only when the if condition is False
  • Never, it's just for documentation
Correct! The else block only runs when the if condition is False. It's like having a backup plan that automatically kicks in.
๐Ÿ” Code Analysis

6. What will this code output when temperature = 25?

temperature = 25 if temperature > 30: print("It's hot!") elif temperature > 20: print("It's warm!") else: print("It's cool!")
  • It's hot!
  • It's warm!
  • It's cool!
  • Nothing will print
Correct! Since 25 > 20 is True, Python executes the first elif block and prints "It's warm!" Python stops checking conditions after finding the first True one.

7. What does this condition check?

if age >= 18 and has_license == True:
  • If age is 18 or has a license
  • If age is 18 and has a license
  • If age is at least 18 and has a license
  • If age is exactly 18 and has a license
Correct! The condition checks if age is at least 18 (>= means greater than or equal to) AND has_license is True. Both conditions must be true.

8. What will happen with this code?

number = 10 if number % 2 == 0: print("Even") else: print("Odd")
  • Prints "Even"
  • Prints "Odd"
  • Prints both "Even" and "Odd"
  • Causes an error
Correct! 10 % 2 = 0, and 0 == 0 is True, so it prints "Even". This is a common way to check if a number is even (remainder 0 when divided by 2).
๐Ÿง  Logical Operators

9. Which logical operator means "both conditions must be True"?

  • or
  • and
  • not
  • if
Correct! The 'and' operator is "the perfectionist" - both conditions must be True for the entire expression to be True.

10. What does the 'not' operator do?

  • Checks if two things are different
  • Flips True to False and False to True
  • Combines two conditions
  • Creates a new variable
Correct! The 'not' operator is like "opposite day" - it flips the boolean value. not True becomes False, and not False becomes True.

11. When is this condition True?

if day == "Monday" or day == "Friday":
  • Only when day is "Monday"
  • Only when day is "Friday"
  • When day is either "Monday" or "Friday"
  • When day is both "Monday" and "Friday"
Correct! The 'or' operator is "the flexible friend" - it's True when at least one condition is True, so either Monday OR Friday will work.
โœ๏ธ Fill in the Blanks

12. Complete this invoice status checker:

invoice_status = "pending" days_past_due = 5 invoice_status == "pending" days_past_due > 0: print("Send reminder") invoice_status == "paid": print("Thank you!") : print("Check status")

Fill in: The keywords needed to create a complete if/elif/else structure.

Correct answers: "if", "and", "elif", "else" - This creates a complete decision structure that checks multiple conditions in order.
โœ… True or False

13. Python checks all elif conditions even after finding one that's True.

  • True
  • False
Correct! False! Python stops checking conditions as soon as it finds the first True one. It's like a decision tree where you only follow one path.

14. You can have multiple elif statements in a single if/elif/else structure.

  • True
  • False
Correct! True! You can have as many elif statements as you need. It's like having a sophisticated decision tree with multiple branches.

15. This condition will always be True:

if 5 > 3 or 5 < 3:
  • True
  • False
Correct! True! Since 5 > 3 is True, and 'or' only needs one condition to be True, this will always be True regardless of the second condition.

๐ŸŽ‰ Quiz Complete!

Your Score: 0/15

Keep practicing!