error-handling - while 循环中出现不支持的操作数类型错误
问题描述
我正在使用两个主要的 while 循环。一种用于输入。其他用于运行测试。
我收到了这个错误:
Error : File "n1.py", line 22, in <module> while j<n-1 and line[a][j+1]-line[a][j]<=2: TypeError: unsupported operand type(s) for -: 'list' and 'int'
当这一行中的所有内容都是整数类型时,为什么我会收到错误消息。
解决方案
一切都不是整数。在第 3 行你有n=[]
(这是一个list)然后在第 21 行你有n-1
您不能在int和list之间使用减法。
这导致:
TypeError: unsupported operand type(s) for -: 'list' and 'int'
也许您想访问列表中的最后一项
n[-1] # Make sure there is at least one item otherwise it throws an IndexError
或者,也许您想获取列表的最后一个索引
len(n) - 1
推荐阅读
- r - 数列因子的计数因子实例
- javascript - 在 Vue 中动态添加带有事件的不同组件
- php - 如何从字符串中删除最后一个逗号和所有空格,然后使用 PHP 对其进行清理?
- javascript - location.href 不起作用(重新加载同一页面)
- ruby - 从左到右列的引导网格布局
- node.js - express.js 在初始加载后重新加载时找不到路由
- json - 查询复杂 CosmosDB 文档的简洁方法
- android - 选择下一个输入后文本消失 - React Native Android
- node.js - 突然开始在 Mac 上遇到带有 Express 服务器的 React App 内部服务器错误 500
- websocket - 外部 WebSocket 服务器上的 Apollo-Server GraphQL 订阅