为什么不加if就会报错
# coding=utf-8
def sum1(n):
return n+sum1(n-1)
print (sum(2))
# coding=utf-8
def sum1(n):
return n+sum1(n-1)
print (sum(2))
2021-03-20
def sums (n):
refult = 0
if n > 0 and n <= 100:
while n > 0 :
refult = refult + n
n -= 1
return refult
else:
return '参数要在1-100之间'
n1 = 99
print(sums(n1))
def fact(n):
if n == 1:
return 1
elif n < 1:
return '参数要在1-100之间'
elif n <= 100:
return n + fact(n-1)
else: # n > 100 的情况
return '参数要在1-100之间'
n2 = 99
print(fact(n2))
我也是刚学python,希望对你有参考作用,你可以像我这样先做判断,就不会报错了
举报