# -*- coding: utf-8 -*-
#python 27
#xiaodeng
#http://www.cnblogs.com/BeginMan/p/3223356.html
#递归2



'非递归方式'
sum=0
#没有sum=0,会出现如下错误提示
'''
Traceback (most recent call last):
  File "C:\Users\Administrator\Desktop\new.py", line 12, in <module>
    sum+=obj
TypeError: unsupported operand type(s) for +=: 'builtin_function_or_method' and 'int'
'''
for obj in range(5):
    sum+=obj
    
print sum        #10


#递归方式
def foo(n):
    if n>0:
        return n+foo(n-1)
    if n<=0:
        return 0

print foo(4)

 

相关文章:

  • 2021-12-06
  • 2022-12-23
  • 2021-04-06
  • 2022-12-23
  • 2021-04-18
  • 2022-12-23
  • 2021-06-05
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-05-19
  • 2021-07-13
  • 2022-12-23
  • 2021-05-29
相关资源
相似解决方案