Lecture 6 - Exercises

6A - Conditional Statements

  1. What does the following program print?

    wwu_founded = 1893
    if (wwu_founded // 1000) < 1:
        wwu_founded = "eighteen ninety three"
    if type(wwu_founded) == type("some text"):
        print("WWU was founded in", wwu_founded)
    else:
        print("Year founded:", wwu_founded)
  2. Some of the following expressions could fill in the blank below to make the following program print oolong. Which ones are they?

    if (True and (_______ or not True)) and not False:
        print("green")
    else:
        print("oolong")
    1. True
    2. False
    3. not True
    4. not False
    5. 3 == 4
    6. 17 % 2 == 1
  3. Is there anything that could fill in the blank in the program above that would cause the program to print both green and oolong?

6B - Nested and Chained Conditionals

Consider the following program.

# assume x and y are numbers
if x < y:
    print("x is less than y")
else:
    if x > y:
        print("x is greater than y")
    else:
        print("x and y must be equal")
  1. How many comparison operators (>, <) are evaluated when the program is run with x set to 4 and y set to 5?
  2. How many comparison operators (>, <) are evaluated when the program is run with x set to 8 and y set to 3?
  3. Rewrite the program to use chained conditionals (i.e., elif) instead of nested if statements.

Consider the following program:

# Program 0:
if (a < 0) == True:
    print(0)
else:
    if a >= 0:
        print(a)
  1. Which of the following programs are equivalent to Program 0? In other words, which of the following has exactly the same output regardless of the value of a? (hint: what values of ‘a’ would you want to check in a test table?)

    # Program 1:
    if a < 0:
        print(0)
    else:
        print(a)
    
    # Program 2:
    if (a < 0) == True:
        print(0)
    print(a)
    
    # Program 3:
    if (a > 0) == True:
        print(a)
    else:
        print(0)
    
    # Program 4:
    if (a < 0) == True:
        print(0)
    if a >= 0:
        print(a)
  2. Of the programs above that are equivalent to 0 (including Program 0 itself), which is the simplest and thus most preferable way to write it?

  3. Consider the following three programs. Give the smallest positive integer value for the variable num_tacos such that the three programs print exactly the same thing when they are executed. Hint: what values would you put in a testing table?

    Program 1:

    if (num_tacos == 32):
        print("32 tacos")
    elif (num_tacos < 32):
        print("Too few tacos")
    elif (num_tacos == 32):
        print("32 tacos")
    elif (num_tacos % 5 == 0):
        print("Oh yes, tacos!")
    else:
        print("Too many tacos") 

    Program 2:

    if (num_tacos == 32):
        print("32 tacos")
    if (num_tacos < 32):
        print("Too few tacos")
    if (num_tacos == 33):
        print("33 tacos")
    if (num_tacos % 5 == 0):
        print("Oh yes, tacos!")
    else:
        print ("Too many tacos")

    Program 3:

    if (num_tacos == 32):
        print("32 tacos")
    else:
        if (num_tacos < 32):
            print("Too few tacos")
        else:
            if (num_tacos == 34):
              print("34 tacos")
            else:
                if (num_tacos % 5 == 0):
                    print("Oh yes, tacos!")
                else:
                    print ("Too many tacos")