首页 > 解决方案 > 如何将所有数字存储在一个名为 numbers 的变量中

问题描述

我决定做一个计算器,所以我需要将数字添加到其他数字中,所以数字必须是一个变量,我怎样才能将所有整数和浮点数存储到一个变量中。

标签: pythonpython-3.x

解决方案


您需要使用列表。这是一个例子。

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)

推荐阅读