python - 仅遍历数组的某个部分的正确方法?
问题描述
Traceback (most recent call last):
File "C:/Users/yaahy/PycharmProjects/Testing/testing1.py", line 45, in
<module>
clicktheshit()
File "C:/Users/yaahy/PycharmProjects/Testing/testing1.py", line 41, in
clicktheshit
pyautogui.click(chords[0], chords[1])
TypeError: 'NoneType' object is not subscriptable
由于我的脚本对每个像素的搜索速度很慢,我想通过删除一些它所查看的无用像素(不在游戏区域中的像素)来加快它的速度,但使用
pxlss = pxls[60:400]
不起作用,我不知道问题所在,因为它可以在不尝试删除无用的东西的情况下工作,只是速度很慢
import pyautogui
import time
from PIL import Image
import mss
import mss.tools
import cv2
import numpy as np
from PIL import ImageGrab
import colorsys
time.sleep(2)
def shootfunc(xc, yc):
pyautogui.click(xc, yc)
gameregion = [71, 378, 328, 530]
def findpixels(pxls):
pxlss = pxls[60:400]
for row, pxl in enumerate(pxlss):
for col, pxll in enumerate(pxl):
if col >= 536 and col <= 808 and row <= 515 and row >= 371 and pxll == (102, 102, 102):
foundpxl = pxll
print(str(col) + " , " + str(row))
return [col, row]
break
def clicktheshit():
with mss.mss() as sct:
region = {'top': 0, 'left': 0, 'width': 1920, 'height': 1080}
imgg = sct.grab(region)
pxls = imgg.pixels
chords = findpixels(pxls)
pyautogui.click(chords[0], chords[1])
xx = 0
while xx <= 3000:
clicktheshit()
xx = xx + 1
time.sleep(.01)
clicktheshit()
解决方案
阅读错误消息和回溯应该会给您第一个提示:
File "C:/Users/yaahy/PycharmProjects/Testing/testing1.py", line 41, in clicktheshit
pyautogui.click(chords[0], chords[1])
TypeError: 'NoneType' object is not subscriptable
这意味着在这个确切的行中,chords
是None
对象 - 当然不能被索引 - 而不是[col, row]
您期望的列表。
现在为什么你得到这个None
而不是预期的列表很简单:你的findpixels
函数只有在它真正找到匹配时才返回这个列表 - 否则,函数最终没有明确的return
声明,所以它隐含地返回None
。
IOW,您的问题与“仅迭代数组的特定部分的正确方法”无关……与不知道如何调试程序有很大关系。
推荐阅读
- html - 如果有一个模式属性,是否还包括必需的属性?
- google-cloud-platform - 使用 cloudsqlproxy 从 GKE 集群连接到 Google 云 mysql 实例
- angular - 使用“dotnet run”命令运行时 Angular/NET Core Web API 未命中
- perl - USPS HTTP 发布请求
- ampl - 下标中的 AMPL 变量尚不允许
- ios - realmdb swift 不从文件中读取值
- c# - 读取 appsettings.json 文件 C# .NET Core 的问题
- javascript - 我无法使用以下 JavaScript 代码制作运行列表
- java - Uiautomator 使用 adb 绑定到坐标
- java - Android:错误:程序类型已存在:com.google.common.base.AbstractIterator$State