python - 在这个简短的 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'
解决方案
您正在拆包scores
:
def z(*scores):
这使它成为一个列表元组(的值scores
将([100, 90, 80, 70, 60],)
在函数内部)。删除以按原样*
使用列表:scores
def z(scores):
推荐阅读
- python - 如何让我的不和谐机器人转发其 dms
- c# - 如何使用 moq 设置 Dispatcher InvokeAsync 方法
- javascript - 如何使响应不渲染也不重定向?
- java - 降级 java/gradle 版本的自制软件
- java - Java连接两个可跨字符串的问题
- r - R:用匹配的条形颜色填充线连接条形图
- javascript - 使用 React Router 的导航测试失败
- objective-c - Swift 中这个 Objective-C 安全类型转换表达式的等价物是什么?
- java - List 无法解析为类型,ArrayList 无法解析为类型
- java - 使用 Spring Oauth2RestTemplate 时如何切换到服务用户