如何让循环运行直到条件等于true

问题描述 投票:0回答:1

我希望代码不断重复,直到它为真

if pyautogui.pixel(1493, 782)[1] == 222: pyautogui.click(1493, 782) else: numpy.repeat()

我希望程序检查它是否是正确的颜色,如果是,我希望它单击它,这是第一部分,但如果它不是正确的颜色,我希望它使用 1 秒睡眠计时器重复代码 直到颜色正确,然后单击这些坐标

我试图输入

numpy.repeat(1)
,但它说我缺少重复 然后我尝试将
numpy.repeats(1)
放入哪个游戏中出现此错误 ",第 347 行,位于 getattr raise AttributeError("模块 {!r} 没有属性" AttributeError:模块“numpy”没有属性“重复”。您的意思是:“重复”吗?

进程已完成,退出代码为 1

python loops repeat pyautogui
1个回答
0
投票

您好,您想使用评论中指出的 while 循环。

例如

import pyautogui
import time

while True:
    # Check the color of the specified pixel
    if pyautogui.pixel(1493, 782)[1] == 222:
        pyautogui.click(1493, 782)  
        break  # Exit the loop once the click is performed
    else:
        time.sleep(1)  # 1 second sleep

我注意到您在评论中询问如何将其变成函数。使用您的代码:

def wait_and_click(x, y, color_value):
    """
    Wait until the pixel color at (x, y) matches the specified color_value, then click it.

    Parameters:
    - x, y: Coordinates of the pixel to check.
    - color_value: The desired color value to match.
    """
    while not pyautogui.pixel(x, y)[1] == color_value:
        time.sleep(1)
    pyautogui.click(x, y)

您可以通过拨打电话进行测试

wait_and_click(1493, 782, 222)

© www.soinside.com 2019 - 2024. All rights reserved.