Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jsf-2/2.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
上市';int';对象在python中是不可编辑的_Python - Fatal编程技术网

上市';int';对象在python中是不可编辑的

上市';int';对象在python中是不可编辑的,python,Python,它不断向我返回错误“int”对象不可编辑。有解决办法吗 def calculate_average_sales(employee_list): for e in employee_list: average = sum(e.sales)/len(e.sales) print("Average sales of all employees: ",average) print(calculate_average_sales(employee_list)) 您正在对

它不断向我返回错误“int”对象不可编辑。有解决办法吗

def calculate_average_sales(employee_list):
    for e in employee_list:
        average = sum(e.sales)/len(e.sales)
    print("Average sales of all employees: ",average)

print(calculate_average_sales(employee_list))

您正在对
int
调用
sum
,但是
sum
的签名要求它将iterable作为第一个参数:

sum(e.sales) # ???
如果您尝试调用
sum
打开,请说
3
,它将引发相同的异常:

>>> sum(3)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'int' object is not iterable

很难相信你没有把代码贴在员工名单上

在NATShell中,您尝试在int上进行itarate-因此,如果我必须对代码进行guss,代码是类似这样的

for e in 120: #you wanted to put here some list , but you actualy send numbers
    do something...
如果你做这样的事情,它应该会起作用:

my_list=[employee_list("name",1000),employee_list("nameb",1000),employee_list("namec",1000),]
然后将其发送到如下函数:

calculate_average_sales(my_list)
还有一件事,
sum
不能处理
int
,所以这可能是你的问题

def calculate_average_sales(employee_list):
    for e in employee_list:
        average = sum(e.sales)/len(e.sales)  #in this line
    print("Average sales of all employees: ",average)

如果没有,请在这里发布完整的代码,这样我们会更乐于助人,因为我们不知道员工名单的确切内容,这是不可能的。将来,一定要发布足够的代码,以便有人可以复制粘贴您拥有的内容并运行它来重现问题。在您的例子中,问题是您假设的列表变量实际上是一个int。
类型(employee_list)
应该返回list。列表如下:def init_employee_list(employee_list):e=SalesEmployee('101','Angie',120015000)employee_list.append(e)e=SalesEmployee('105','Cindy',100012000)employee_list.append(e) e=SalesEmployee('108','David',150020000)员工名单。append(e)e=SalesEmployee('112','Jason',30000)员工名单。append(e)e=SalesEmployee('127','Vivian',200025000)员工名单。append(e)@Gohboonhuay似乎已经回答了你的问题,所以你应该接受答案并结束这个问题。
def calculate_average_sales(employee_list):
    for e in employee_list:
        average = sum(e.sales)/len(e.sales)  #in this line
    print("Average sales of all employees: ",average)