方法一:
def power(x,y):
return x ** y
方法二:
def power(x,y):
if y:
result = 1
for i in range(y):
result *= x
else:
return 1
return result
number = int(input('请输入一个正整数:'))
power_value = int(input('请输入幂值:'))
result = power(number,power_value)
print("%d 的 %d 次幂是:%d" % (number,power_value,result))
方法三:
#递归
def power(x,y):
if y:
return x * power(x,y-1)
else:
return 1
number = int(input('请输入一个正整数:'))
power_value = int(input('请输入幂值:'))
result = power(number,power_value)
print("%d 的 %d 次幂是:%d" % (number,power_value,result))