HomepageTeaching PageResearch PageResources Page

Homework: Conditionals

Read sections 5.4 and 5.5 in the book and do the examples.

Now we can improve our function from last time to give a more detailed output.

def positive_and_divisible_by_3(x):
    """Prints whether x is divisible by 3 and positive."""
    if (x % 3 == 0) and (x > 0):
        print("Yes,", x, "is divisible by 3 and positive.")
    else:
        print("No,", x, "is either not divisible by 3 or is not positive.")
        

This output is far easier to read:

>>> positive_and_divisible_by_3(123)
Yes, 123 is divisible by 3 and positive.
>>> positive_and_divisible_by_3(-1231)
No, -1231 is either not divisible by 3 or is not positive.
>>> positive_and_divisible_by_3(37)
No, 37 is either not divisible by 3 or is not positive.
>>> positive_and_divisible_by_3(-6)
No, -6 is either not divisible by 3 or is not positive.
>>> positive_and_divisible_by_3(0)
No, 0 is either not divisible by 3 or is not positive.