小编HER*_*848的帖子

Python - 如何添加整数(可能在列表中?)

我正在为学校做作业,我必须让“用户”输入一个整数列表,然后程序必须将列表中的整数相加并返回:

Total: $[sum of integers]
Run Code Online (Sandbox Code Playgroud)

到目前为止,我有

cost = input("Enter the expenses: ")
cost = int(cost)
total = sum(i)
print("Total: $" + i)
Run Code Online (Sandbox Code Playgroud)

但它不断返回错误:

Traceback (most recent call last):
  File "C:\Python33\Did I Spend Too Much.py", line 2, in <module>
    cost = int(cost)
ValueError: invalid literal for int() with base 10: '10 15 9 5 7'
Run Code Online (Sandbox Code Playgroud)

其中“10 15 9 5 7”是我在测试中输入的整数。

对此的任何帮助将不胜感激

python variable-assignment

5
推荐指数
0
解决办法
3838
查看次数

标签 统计

python ×1

variable-assignment ×1