当前位置:网站首页>Realize the addition of all numbers between 1 and number

Realize the addition of all numbers between 1 and number

2022-07-05 13:01:00 South Lake Fishing Song

Write a function :
The name is called sum_numbers
There is a parameter called number
Within the function , Realization 1~number Between , Sum of all numbers
Print the result of the addition
Call this function many times , Introduce different number View results


# While  loop 
def sum_number(number):
    ''' MAS: Realization  1~number  Between , Sum of all numbers  '''
    i = 1
    sum_value = 0
    while i < number + 1:
        sum_value = sum_value + i
        i = i + 1

    print(number,' The result of addition is ',sum_value)
sum_number(100)

# for  loop 
def sum_number(number):
    ''' MAS: Realization  1~number  Between , Sum of all numbers  '''
    sum_value = 0
    for i in range (1,number + 1):
        sum_value += i
    print(number,' The result of addition is ',sum_value)
sum_number(100)

原网站

版权声明
本文为[South Lake Fishing Song]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/186/202207051240052899.html