微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何在 IF 中使用结果来吐出 True/False?

如何解决如何在 IF 中使用结果来吐出 True/False?

以下代码片段

if pyautogui.locateOnScreen('test4x4.png',region = (200,200,4,4)) == "Box(left=200,top=200,width=4,height=4)":
    print("Found")
else:
    print("NotFound")

吐出NotFound, 但是

print(pyautogui.locateOnScreen('test4x4.png',4)))

打印出“Box(left=200,height=4)”。 如何正确匹配结果以便我可以取回 Found 值?

解决方法

boolean isListPalindrome(ListNode<Integer> l) { if(l == null || l.next == null)return true; List<Integer> list = new ArrayList<Integer>(); List<Integer> revlist = new ArrayList<Integer>(); while(l != null){ list.add(l.value); revlist.add(l.value); l = l.next; } Collections.reverse(revlist); return list.equals(revlist); } 函数返回一个 locateOnScreen 对象,不是一个字符串。

因此,您需要在进行任何比较之前将其转换为元组:

pyscreeze.Box

您得到 box = pyautogui.locateOnScreen('test4x4.png',region = (200,200,4,4)) if box is not None and tuple(box) == (200,4): print("Found") else: print("NotFound") 的原因是因为如果找不到图像,TypeError: 'NoneType' object is not iterable 会返回 locateOnScreen。尝试将 None 转换为元组:None 会引发错误。

您可以通过选中该框不是 tuple(None) 来防止这种情况发生,正如我在上面编辑的那样。

我无法解释为什么在成功找到图像后您仍然收到错误消息,因此您需要提供更多信息以便我们解决该问题。



您的版本不起作用的原因是当您调用 None 时,print(box) 函数实际上在幕后调用 print 并打印出来。

尽管如此

str(box)

意味着

>>> print(box)
Box(left=200,top=200,width=4,height=4)

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。