Using the Python language, have the function SimpleAdding(num) add up all the numbers from 1 to num. For the test cases, the parameter num will be any number from 1 to 1000.
def SimpleAdding(num):
# code goes here
if num > 1:
return num + SimpleAdding(num-1)
else:
return 1
# keep this function call here
# to see how to enter arguments in Python scroll down
print SimpleAdding(raw_input()) Input =
12Output = 78
本文介绍了一个使用Python语言实现的递归函数,该函数能够计算从1到指定数值的所有整数之和。通过定义一个名为SimpleAdding的函数,采用递归的方式进行累加,实现了对于任意1到1000范围内的整数的求和。

被折叠的 条评论
为什么被折叠?



