python - 如何将所有数字存储在一个名为 numbers 的变量中
问题描述
我决定做一个计算器,所以我需要将数字添加到其他数字中,所以数字必须是一个变量,我怎样才能将所有整数和浮点数存储到一个变量中。
解决方案
您需要使用列表。这是一个例子。
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)
推荐阅读
- javascript - React .js TypeError:无法读取未定义的属性“名称”
- swift - 自定义属性的可读显示 (IB) 名称
- android - InputMethodService.onUpdateSelection 是异步的吗?
- c# - 如何从存储在数据库中的 JSON 字符串中检索字段
- c# - while 循环保持运行,即使超出了它的限制
- dc.js - dc.js - 排序数字条形图
- numpy - 由于 Numpy 返回对象而导致的值错误
- python - 强制 subprocess.Popen 使用 write() 函数而不是 fileno() 将 stdout/stderr 写入 python 中的类文件对象
- c# - 找不到使用 WinML 指定的 ONNX 文件
- azure-cosmosdb - Gremlin 上的动态 WITHIN 子句