python-3.x 如何将所有数字存储在一个名为numbers的变量中

mwg9r5ms  于 2023-05-19  发布在  Python
关注(0)|答案(1)|浏览(102)

我决定做一个计算器,所以我需要把数字加到其他数字上,所以数字必须是一个变量,我怎么能把所有的整数和浮点数都存储到一个变量中呢?

k2fxgqgv

k2fxgqgv1#

你必须使用一个列表。这里有一个例子。

number_list = [1, 2, 3, 4, 5]

def addition(number_list):
    result = 0
    for number in number_list:  # the for loop will iterate every number of your list one by one
        result += number  # Which does the same as result = result + number
    return result

# if you want to see the result in the terminal, you can print the result with:
print(addition(number_list))

相关问题