Skip to main content

Section 5.3 Assessment: Conditionals

Subgoals for writing conditionals:.

  1. Define how many mutually exclusive paths are needed
  2. Order paths from most restrictive to least restrictive
  3. Write conditional statement with Boolean expression
  4. Follow with true path including action in indentation
  5. Repeat 3 and 4 until all paths are included
  6. (Optional) Follow with else path including action in indentation

Exercises Exercises

    1.

      Q1: What is the output of the following code?
      a = 10
      b = 20
      if 2 * a >= b:
          print("Yes")
      else
          print("No")
      
    • Yes
    • No
    • There is no output because a compiler error occurs
    • There is no output because an exception occurs

    2.

    3.

    Q3: What is the output of the following code?
    if 5 == (10 // 2):
        if 5 % 2 > 0:
            if 0:
                conditions = 0
            else:
                conditions = 5
        else:
            conditions = 10
    else:
        conditions = 15
    if conditions:
        if conditions % 2 > 0:
            print("Odd")
        else:
            print("Even")
    else:
        print("Conditions not met")
    

    4.

    Q4: Enter the value of each variable after the following code is executed:
    a = 0
    b = 1
    c = 2
    if a < c:
        a = c
    if 2 * b == c:
        c = 0
    elif 2 * b < c:
        b = 0
    else:
        a = 0
    if c:
        c = 2*(b + a)
    elif b:
        b = 2*(a + c)
    elif a:
        a = 2*(b + c)
    
    The value of variable a is , the value of variable b is , and the value of variable c is .

    5.

    Put the code in the right order to create a program that will ask the user for a number between -20 and 20 (inclusive).Print the number. Then check if the number is positive, negative, or neither. Print the result.

    6.

    Put the code in the right order to create a program that will generate a random number (1 to 100 inclusive), and print the number. Check if the number is a multiple of 2 and print the result. Then check if the number is a multiple of 3 and print the result. Then check if the number is a multiple of 5 and print the result.
You have attempted of activities on this page.