Question: Background to this question Functions can return booleans, which is often convenient for hiding complicated tests inside functions. For example: def is_divisible(x, y): if x

Background to this question

Functions can return booleans, which is often convenient for hiding complicated tests inside functions. For example:

def is_divisible(x, y): if x % y == 0:

return True

else: return False

It is common to give boolean functions names that sound like yes/no questions; is_divisible returns either True or False to indicate whether x is divisible by y. Here is an example: >>> is_divisible(6, 4)

False

>>> is_divisible(6, 3)

True

The result of the == operator is a boolean, so we can write the function more concisely by returning it directly: def is_divisible(x, y): return x % y == 0 Boolean functions are often used in conditional statements: if is_divisible(x, y): print('x is divisible by y') It might be tempting to write something like: if is_divisible(x, y) == True: print('x is divisible by y') But the extra comparison is unnecessary.

As an exercise, write a function is_between(x, y, z) that returns True if x y z or False otherwise.

The QUESTION

Do Exercise 6.4 from your textbook using recursion and the is_divisible function from Section 6.4. Your program may assume that both arguments to is_power are positive integers. Note that the only positive integer that is a power of "1" is "1" itself. After writing your is_power function, include the following test cases in your script to exercise the function and print the results:

print("is_power(10, 2) returns: ", is_power(10, 2))

print("is_power(27, 3) returns: ", is_power(27, 3))

print("is_power(1, 1) returns: ", is_power(1, 1))

print("is_power(10, 1) returns: ", is_power(10, 1))

print("is_power(3, 3) returns: ", is_power(3, 3))

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!