Discuss / Python / 关于mypower函数的改善

关于mypower函数的改善

Topic source

#1 Created at ... [Delete] [Delete and Lock User]

def mypower (x,n):

    if not isinstance(n,int):

        raise TypeError('bad operand type')

    s=1

    if n>0:

        while n>0:

            n=n-1

            s=s*x

        return s

    if n<0:

        while n<0:

            n=n+1

            s=s*(1/x)

        return s

a=mypower(2,-2)

print(a)


  • 1

Reply