我需要编写一个程序,在其中编写一个迭代函数来计算基 * 指数的指数,而无需在程序中使用 ** 运算符。
我已经尝试了我已经创建的代码,但不确定如何修复“int”对象不可调用的错误。
def iterPower (base, exp):
"""Run a program in which the base multiplies itself by the exponent value"""
exp = 3
for n in base(exp):
exp *= base
return exp
base = 5
exp = 3
print(iterPower(5,3))
预期结果是 125,但由于我的错误,我没有得到任何数字。
最佳答案
您需要乘以 base * base
exp
次:
def iterPower (base, exp):
"""Run a program ion which the base multiplies itself by the exponent value"""
n = base
for _ in range(1, exp):
n *= base
return n
结果:
>>> iterPower(5, 3)
125
>>> 5**3
125
关于python - 不使用 ** 计算指数的迭代函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55841135/