For an exercise, I have to complete a code that demonstrates the recursion in python.
I have been given and code and told to complete it so for example, that 4^2 = 16
def raise_to_power(base_val, exponent_val):
if exponent_val == 0:
result_val = 1
else:
result_val = base_val * #start entering code here
return result_val
user_base = 4
user_exponent = 2
print('%d^%d = %d' % (user_base, user_exponent,
raise_to_power(user_base, user_exponent)))
I know what they want me to do, but I don't know how to express it. I tried doing base_val ** exponent_val, but it says invalid syntax. It also says not to use math.pow() function.
Can someone help me understand this? I am finding recursion very difficult to get.
raise_to_powerneeds to call itself at some point again. Maybe this (cs.utah.edu/~germain/PPS/Topics/recursion.html) helps to understand the concept.