如何将所有数字存储在一个名为numbers的变量中

2024-04-19 17:23:37 发布

您现在位置:Python中文网/ 问答频道 /正文

我决定做一个计算器,所以我需要把数字和其他数字相加,所以数字必须是一个变量。我怎样才能把所有的整数和浮点数存储到一个变量中呢。你知道吗


Tags: 数字整数计算器浮点数
1条回答
网友
1楼 · 发布于 2024-04-19 17:23:37

你需要使用一个列表。下面是一个例子。你知道吗

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)

相关问题 更多 >