首页 > 解决方案 > 在这个简短的 py 代码中,什么触发了“+: 'int' 和 'list' 不支持的操作数类型”错误?我该如何解决

问题描述

def z(*scores):
    a=min(scores)
    b=max(scores)
    c=sum(scores)/len(scores)
    print(f"min : {a}\nmax : {b}\naverage : {c}")

y = input("type all scores. ex)100, 90, 80, 70, 60\n")
x = y.split(", ")
x = list(map(int, x))
z(x)

当我运行它时,它会触发此错误这是怎么回事,我该如何解决

type all scores. ex)100, 90, 80, 70, 60
100, 90, 80, 70, 60
Traceback (most recent call last):
  File "c:\Users\____\Desktop\a.py", line 11, in <module>
    z(x)
  File "c:\Users\____\Desktop\a.py", line 5, in z
    c=sum(scores)/len(scores)
TypeError: unsupported operand type(s) for +: 'int' and 'list'

标签: python

解决方案


您正在拆包scores

def z(*scores):

这使它成为一个列表元组(的值scores([100, 90, 80, 70, 60],)在函数内部)。删除以按原样*使用列表:scores

def z(scores):

推荐阅读