Python:求阶乘

求数字5的阶乘

这里用了两种方法,方法一

#coding=utf-8
Product=1
for i in range(1,7):
    Product*=i
    i+=1

print(Product)


方法二:用函数的方法求阶乘

#coding=utf-8

def factorial(n):

    if not isinstance(n,int):
        raise TypeError("The input is not Int Type")#函数应检查参数的类型
    Result=1
    
    for i in range(1,n+1):
        Result*=i
    return Result


print(factorial(5))

print(factorial("5"))


方法一,在运行时报了错误:IndentationError: unindent does not match any outer indentation level

检查发现实product=1,缩进不对。这个错也就是告诉你,缩进出了错误。

猜你喜欢

转载自blog.csdn.net/sinat_18722099/article/details/80946534
今日推荐