Title: Testing two conditions in python. One condition is always true even when it is not supposed to be
Tags: python;function;if-statement;recursion;runtime-error
Question: I am having difficulties getting this code to work. I am attempting to do the following.
When x is raised to a power of y, it needs to be true except with number 1.
Return true when x is divisible by y.
Problems.
I wrote this code but the problem is that when I input x=3 and y=3 I get and answer that x is the power of y which isn't true. Regardless of the number, if x == y it always says that x is power of y.
Question.
How would you make the corrections for this code?
def is_divisible(x, y):
if x % y == 0:
print(&quot;x is divisible by y&quot;)
return True
else:
print(&quot;x is not divisible by y&quot;)
print(&quot;x is not the power of y&quot;)
return False
def is_power(x, y):
if x == 1 or y == 1:  # code to test the base of 1
return x == 1
elif x % y == 0:
print(&quot;x is the power of y&quot;)
print(&quot;x is divisible by y&quot;)
elif is_divisible(x, y) and is_power(x / y, y): # recursion of is_divisible. This will be true only if x/y
# then x is the power of y.
return 0.0
else:
return False
print(is_power(2, 2))
outputs
x is the power of y
x is divisible by y
print(is_power(27, 3))
x is the power of y
x is divisible by y
Comment: is_power must be true only if x = 27 is the power or y = 3 ( 3*3*3 = 27) with the exception of 1 which power of 1 is always 1. In this case, when I put x=2, y=2, it says x is the power of y but it is not true. 2 cant be the power of 2. that is the part I cant figure it out. Everything else seems to be working fine. I really want to answer the other question about the return 0.0 but Im fairly new to this and I am doing only what I have learned so far.

Comment: What is `is_power` supposed to test? Regardless, when `x % y == 0` (i.e., when `y` divides `x` evenly), you don't return anything; you just fall through to the end of the function where `None` is implicitly returned. Also, why does the function return `0.0` instead of a boolean value in the third case?



{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 0, 'CommentCount': 2, 'ContentLicense': 'CC BY-SA 4.0', 'CreationDate': '2021:00:04/30/21', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<python><function><if-statement><recursion><runtime-error>', 'Title': 'Testing two conditions in python. One condition is always true even when it is not supposed to be', 'ViewCount': 76, 'id': 67338435, 'user_AboutMe': '', 'user_AccountId': 21377465.0, 'user_DisplayName': 'Edy Molina', 'user_Reputation': 47.0, 'user_Views': 5.0, 'user_id': 15743941.0}}