Book a free demo

๐Ÿ Python from scratch ยท Session 4

Making decisions: if, elif and else: practice sheet

Name ____________________Date __________Score ____ / 25

โ† Back to the lesson

Part 1: Questions with True or False answers

Remember: A comparison gives True or False. == asks "equal?", != "not equal?", and < > <= >= compare sizes.

  1. 1. Warm-up x = 15. Print whether x is greater than 10, then whether x equals 15.

    x = 15
    
  2. 2. Core a = 7 and b = 7. Print whether they are equal, then whether they are different.

    a = 7
    b = 7
    
  3. 3. Stretch ๐Ÿ”ฅ n = 24. Print whether n is even, then whether n is a multiple of 3.

    n = 24
    

Part 2: if and else

Remember: if condition: runs the indented lines only when the condition is True; else: runs when it is False.

  1. 4. Warm-up temp = 30. Print Hot if temp is above 25, otherwise Not hot.

    temp = 30
    
  2. 5. Core n = 7. Print even or odd.

    n = 7
    
  3. 6. Stretch ๐Ÿ”ฅ pin = 4821. Print Access granted if pin is 4821, otherwise Wrong PIN, then always print Goodbye.

    pin = 4821
    

Part 3: Many choices with elif

Remember: elif adds more cases between if and else. Python checks from the top and runs only the first True one.

  1. 7. Warm-up speed = 45. Print Slow below 30, Normal below 60, otherwise Fast.

    speed = 45
    
  2. 8. Core n = -4. Print positive, negative or zero.

    n = -4
    
  3. 9. Stretch ๐Ÿ”ฅ Ticket price by age: under 5 free (Price: 0), under 13 Price: 100, 60 or over Price: 120, everyone else Price: 200.

    age = 30
    

Part 4: and, or, not

Remember: and is True only when both sides are True; or when at least one is; not flips True and False.

  1. 10. Warm-up day = "Sunday". Print Holiday! on Saturday or Sunday, otherwise School day.

    day = "Sunday"
    
  2. 11. Core Print Ride if height is at least 120 and age at least 8, otherwise No ride.

    height = 130
    age = 9
    
  3. 12. Stretch ๐Ÿ”ฅ Leap year, full rule: divisible by 4 and not by 100, or divisible by 400. Print leap or not leap.

    y = 2024
    

Activity 1: The grade machine

  1. 13. Print Grade: A for 90 or more, B for 75 to 89, C for 50 to 74 and D below 50. It will be tested with many marks.

    1. Write if marks >= 90: and print Grade: A.
    2. Add elif for B (75 or more).
    3. Add elif for C (50 or more).
    4. Add else for D.
    5. Test 90, 75, 50 and 49 yourself.
    marks = 82
    
    # Step 1 to 4
    

Activity 2: Electricity bill

  1. 14. The first 100 units cost 5 each, the next 100 cost 7 each and every unit above 200 costs 10. Print Bill: and the amount. Test with 80, 150, 200 and 250 units.

    1. If units <= 100: bill = units ร— 5.
    2. elif units <= 200: bill = 500 + (units โˆ’ 100) ร— 7.
    3. else: bill = 500 + 700 + (units โˆ’ 200) ร— 10.
    4. Print Bill: and the bill.
    units = 150
    
    # Steps 1 to 3: work out bill
    
    # Step 4
    

Activity 3: Rock, paper, scissors judge

  1. 15. p1 and p2 hold rock, paper or scissors. Print Draw, Player 1 wins or Player 2 wins. Rock beats scissors, scissors beat paper, paper beats rock.

    1. If p1 == p2 print Draw.
    2. elif p1 beats p2 (three cases joined with or) print Player 1 wins.
    3. else print Player 2 wins.
    4. Test all nine pairs if you can.
    p1 = "rock"
    p2 = "scissors"
    
    # Step 1
    
    # Step 2
    
    # Step 3
    

Predict the output

Work it out on paper first, then type it into the lesson page to check.

  1. 16. What does this print?

    print(5 > 3)
  2. 17. What does this print?

    x = 7
    if x % 2 == 0:
        print("even")
    else:
        print("odd")
  3. 18. What does this print?

    t = 20
    if t > 30:
        print("hot")
    elif t > 15:
        print("warm")
    else:
        print("cold")
  4. 19. What does this print?

    print(True and False)
  5. 20. What does this print?

    print(not (3 == 3))
  6. 21. What does this print?

    age = 15
    if age >= 13 or age == 9:
        print("yes")
    else:
        print("no")
  7. 22. What does this print?

    n = 12
    r = ""
    if n > 10:
        r = r + "A"
    if n > 5:
        r = r + "B"
    print(r)

Homework

  1. 23. Warm-up year = 2024. Print leap if it is divisible by 4, else not leap (simple rule).

    year = 2024
    
  2. 24. Core a, b and c are three numbers. Print the largest.

    a = 4
    b = 9
    c = 2
    
  3. 25. Stretch ๐Ÿ”ฅ Three sides of a triangle. Print equilateral, isosceles or scalene (or not a triangle if one side is at least the sum of the other two).

    a = 3
    b = 4
    c = 5
    

Answer key

  1. x = 15
    print(x > 10)
    print(x == 15)
  2. a = 7
    b = 7
    print(a == b)
    print(a != b)
  3. n = 24
    print(n % 2 == 0)
    print(n % 3 == 0)
  4. temp = 30
    if temp > 25:
        print("Hot")
    else:
        print("Not hot")
  5. n = 7
    if n % 2 == 0:
        print("even")
    else:
        print("odd")
  6. pin = 4821
    if pin == 4821:
        print("Access granted")
    else:
        print("Wrong PIN")
    print("Goodbye")
  7. speed = 45
    if speed < 30:
        print("Slow")
    elif speed < 60:
        print("Normal")
    else:
        print("Fast")
  8. n = -4
    if n > 0:
        print("positive")
    elif n < 0:
        print("negative")
    else:
        print("zero")
  9. age = 30
    if age < 5:
        print("Price: 0")
    elif age < 13:
        print("Price: 100")
    elif age >= 60:
        print("Price: 120")
    else:
        print("Price: 200")
  10. day = "Sunday"
    if day == "Saturday" or day == "Sunday":
        print("Holiday!")
    else:
        print("School day")
  11. height = 130
    age = 9
    if height >= 120 and age >= 8:
        print("Ride")
    else:
        print("No ride")
  12. y = 2024
    if (y % 4 == 0 and y % 100 != 0) or y % 400 == 0:
        print("leap")
    else:
        print("not leap")
  13. marks = 82
    if marks >= 90:
        print("Grade: A")
    elif marks >= 75:
        print("Grade: B")
    elif marks >= 50:
        print("Grade: C")
    else:
        print("Grade: D")
  14. units = 150
    if units <= 100:
        bill = units * 5
    elif units <= 200:
        bill = 500 + (units - 100) * 7
    else:
        bill = 500 + 700 + (units - 200) * 10
    print("Bill:", bill)
  15. p1 = "rock"
    p2 = "scissors"
    if p1 == p2:
        print("Draw")
    elif (p1 == "rock" and p2 == "scissors") or (p1 == "scissors" and p2 == "paper") or (p1 == "paper" and p2 == "rock"):
        print("Player 1 wins")
    else:
        print("Player 2 wins")
  16. True
  17. odd
  18. warm
  19. False
  20. False
  21. yes
  22. AB
  23. year = 2024
    if year % 4 == 0:
        print("leap")
    else:
        print("not leap")
  24. a = 4
    b = 9
    c = 2
    if a >= b and a >= c:
        print(a)
    elif b >= c:
        print(b)
    else:
        print(c)
  25. a = 3
    b = 4
    c = 5
    if a >= b + c or b >= a + c or c >= a + b:
        print("not a triangle")
    elif a == b and b == c:
        print("equilateral")
    elif a == b or b == c or a == c:
        print("isosceles")
    else:
        print("scalene")
Chat on WhatsApp
Esc

Try