python-3.x - 为什么,只要if子句成立,值就变了;如果在python中,值超出条件时将重新归零
问题描述
我希望每次调用对象函数时,将 s 的值添加到一个单元中,但是在这段代码中,只要添加了一个单元,它的值就会重置为零,并且只要条件成立它的值变为 1,退出时它的值为零,而我希望每次设置 if 子句时,将 s 添加到 1,例如 1、2 和 ..
def objects(x,y,startx,starty,startw,starth,objectw,action=None):
s=0
if y < starty + starth :
if x <= startx + startw and x + objectw > startx + startw or x + objectw == startx + startw or x + objectw >= startx and x<= startx or x > startx and x + objectw < startx + startw:
s+ =1
解决方案
我不确定我是否理解,但请尝试在函数范围之外定义变量 s。就像现在一样,每次调用 objects() 函数时,它首先将 s 重新定义为 0。
s = 0
def objects(x,y,startx,starty,startw,starth,objectw,action=None):
if y < starty + starth :
if x <= startx + startw and x + objectw > startx + startw or x + objectw == startx + startw or x + objectw >= startx and x<= startx or x > startx and x + objectw < startx + startw:
s+=1
推荐阅读
- flutter - “_drawAreaBoundsOutdated == false': is not true”使用 charts_flutter 包更新 TimeSeriesChart 的列表数据时出错
- amazon-web-services - Glue Classifier 无法使用 Grok 模式对列进行分类
- mybatis - MyBatis:无效比较:java.util.Date 和 java.lang.String
- python - 在外部库中使用时禁用 tensorflow 日志
- javascript - [{a: 1}],[{b: 2}] 转换为 {a:1,b:2}
- python - Pandas get cell value by row NUMBER (NOT row index) and column NAME
- sql - Oracle 10g 和 11g 中的 Regexp_like
- c - 传递 'atoi' 的参数 1 从整数中生成指针,而无需在 C 中进行强制转换
- sequelize.js - 是否可以使用 sequelize 中的钩子获取插入行的 ID?
- java - 缺少return语句,但是return语句在那里?