Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 我的总数=总数+;b(在b:中代表e时)不会把它们加在一起吗?_Python_Python 3.x - Fatal编程技术网

Python 我的总数=总数+;b(在b:中代表e时)不会把它们加在一起吗?

Python 我的总数=总数+;b(在b:中代表e时)不会把它们加在一起吗?,python,python-3.x,Python,Python 3.x,这里是新的学习者,但我的代码似乎不起作用,需要注意的是,通过查看错误检查和语法错误等,我厌倦了一切。如果有人能给我一个解决方案那就太好了。谢谢:)您将在总数中添加b,但b是列表。您应该从列表中添加一个元素,即,在代码中,e 因此,它应该是total=total+e这里的问题是不能在int和list之间使用操作数+ 您可能看到的是一个类型错误,表明了这一点 from typing import List b: List[int] = [52, 58, 15, 83] total = 0 for

这里是新的学习者,但我的代码似乎不起作用,需要注意的是,通过查看错误检查和语法错误等,我厌倦了一切。如果有人能给我一个解决方案那就太好了。谢谢:)

您将在总数中添加b,但b是列表。您应该从列表中添加一个元素,即,在代码中,e


因此,它应该是
total=total+e

这里的问题是不能在
int
list
之间使用操作数
+

您可能看到的是一个
类型错误
,表明了这一点

from typing import List

b: List[int] = [52, 58, 15, 83]
total = 0

for e in b:
    total = total + b
print(total)
如果将
+
反转为
b+总计
,则会返回不同的
类型错误

TypeError: unsupported operand type(s) for +: 'int' and 'list'

在循环中,您没有使用在循环中定义的变量访问列表中的元素:
e
。这将允许您利用
+
,因为
total
e
都是整数

TypeError: can only concatenate list (not "int") to list
您还可以使用
sum()
来计算列表的总值,而无需使用循环

for e in b:
    total = total + e

    #208

是的,谢谢你的邀请。
sum(b)
#208