求解for - str and int 代碼如何修改
n=input()
def fact(n):
if n==1:
return 1
return n*fact(n-1)
print(fact(n))
錯誤在n*fact(n-1),如何修改能達到我要的效果;
輸入數字輸出計算結果
n=input()
def fact(n):
if n==1:
return 1
return n*fact(n-1)
print(fact(n))
錯誤在n*fact(n-1),如何修改能達到我要的效果;
輸入數字輸出計算結果
2019-02-14
舉報
2019-02-14
這里第五行需要轉換一下,因為ptyhon不想像其他語言,需要指定數據類型,錯誤提示說的是str與int不能用‘-’號運算,我猜測可能是與函數返回值的類型有關系,我也是個小白,有什么說錯的地方還請見諒!
2019-02-14
n=input()
def fact(n):
????if n==1:
????????return 1
????return n*fact(n-1)
print(fact(n))