Python returns 没有
Python returns nothing
我有一个奇怪的问题。当我 运行 下面的代码并且图像不存在时,代码应该 return "None" 但由于某种原因没有打印任何内容。
PS:当我在虚拟机中 运行 这段代码一切正常,None 是 returned。
try:
inv_settings = pyautogui.locateOnScreen("settings.png", confidence=0.99)
except Exception:
pass
编辑:
import pyautogui
settings = not None
while settings is not None:
try:
inv_settings = pyautogui.locateOnScreen("settings.png", confidence=0.99)
print(inv_settings)
except Exception:
pass
print("finished")
如果你想这样做 returns None
无论如何,只需删除 print
.
def find():
try:
inv_settings = pyautogui.locateOnScreen("settings.png", confidence=0.99)
except Exception:
pass
Python 解释器不会打印出 None
,因为这意味着没有任何有用的东西可以显示,但您仍然可以打印出实际的 return 值。
>>> find()
>>> # nothing was printed out because find() returned None
>>> print(find())
None
>>>
编辑:此外,您真的不应该禁止所有异常,因为这可能会阻止代码中的实际错误。在这种情况下,如果找到,则应设为 return True
,否则应设为 False
。否则,只是浪费时间和精力给出一个什么都不能告诉你的结果。
EDIT2:看起来您已经用完整代码更新了问题。我将为您提供更新后的代码和评论。
# all imports at the top :)
import pyautogui
# set `location` to None
location = None
# i'm assuming you want to keep locating until you find it so keep on
# checking until location isn't None anymore
while location is None:
# i've checked the documentation and it seems that
# pyautogui.locateOnScreen either returns the location in
# a named tuple `Box(left, top, width, height)` or it
# raises pyautogui.ImageNotFoundException
# here we try to get the location of "settings.png" on our screen
try:
location = pyautogui.locateOnScreen("settings.png", confidence=0.99)
# we catch the exception if it can't find it. note we don't catch
# all exceptions as it might hide other problems.
except pyautogui.ImageNotFoundException:
pass
print("finished")
我有一个奇怪的问题。当我 运行 下面的代码并且图像不存在时,代码应该 return "None" 但由于某种原因没有打印任何内容。
PS:当我在虚拟机中 运行 这段代码一切正常,None 是 returned。
try:
inv_settings = pyautogui.locateOnScreen("settings.png", confidence=0.99)
except Exception:
pass
编辑:
import pyautogui
settings = not None
while settings is not None:
try:
inv_settings = pyautogui.locateOnScreen("settings.png", confidence=0.99)
print(inv_settings)
except Exception:
pass
print("finished")
如果你想这样做 returns None
无论如何,只需删除 print
.
def find():
try:
inv_settings = pyautogui.locateOnScreen("settings.png", confidence=0.99)
except Exception:
pass
Python 解释器不会打印出 None
,因为这意味着没有任何有用的东西可以显示,但您仍然可以打印出实际的 return 值。
>>> find()
>>> # nothing was printed out because find() returned None
>>> print(find())
None
>>>
编辑:此外,您真的不应该禁止所有异常,因为这可能会阻止代码中的实际错误。在这种情况下,如果找到,则应设为 return True
,否则应设为 False
。否则,只是浪费时间和精力给出一个什么都不能告诉你的结果。
EDIT2:看起来您已经用完整代码更新了问题。我将为您提供更新后的代码和评论。
# all imports at the top :)
import pyautogui
# set `location` to None
location = None
# i'm assuming you want to keep locating until you find it so keep on
# checking until location isn't None anymore
while location is None:
# i've checked the documentation and it seems that
# pyautogui.locateOnScreen either returns the location in
# a named tuple `Box(left, top, width, height)` or it
# raises pyautogui.ImageNotFoundException
# here we try to get the location of "settings.png" on our screen
try:
location = pyautogui.locateOnScreen("settings.png", confidence=0.99)
# we catch the exception if it can't find it. note we don't catch
# all exceptions as it might hide other problems.
except pyautogui.ImageNotFoundException:
pass
print("finished")