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,縮進不對。這個錯也就是告訴你,縮進出了錯誤。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章