Python 计算指数的迭代函数**

Python 计算指数的迭代函数**,python,python-3.x,function,Python,Python 3.x,Function,我需要编写一个程序,在这个程序中,我编写了一个迭代函数来计算基*指数的指数,而不需要在我的程序中使用**运算符 我已经尝试了我已经创建的代码,但不确定如何修复“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

我需要编写一个程序,在这个程序中,我编写了一个迭代函数来计算基*指数的指数,而不需要在我的程序中使用**运算符

我已经尝试了我已经创建的代码,但不确定如何修复“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(exp)那样调用5(3)。尝试使用范围内的n(exp),它将为您提供所需的迭代次数。

您需要多次
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