【问题标题】:"unexpected unindent" for correct indentation?正确缩进的“意外缩进”?
【发布时间】:2019-07-28 12:23:41
【问题描述】:

尝试在 python 中制作自动点击器,但进展不顺利。一旦我认为我终于做对了,它会说unexpected unindent.

缩进似乎是正确的?我看不出有什么问题。

另外,如果你们能就如何使我的代码更好,我会很高兴,因为我是这方面的初学者:/

import pyautogui, mouse, keyboard, sys

delay=0.01

while True:
    try:
        if keyboard.is_pressed("q"):
            sys.exit()
def left_mouse():
    while True:
        pyautogui.click()
        pyautogui.delay=delay
mouse.on_click(left_mouse)

在 IDLE 中,红色错误指示器显示在 sys.exit() 的右侧 不过那里似乎没有问题?

【问题讨论】:

标签: python indentation


【解决方案1】:

在 python 中,try: 块需要至少有一个 except:finally: 块跟随它。如果您想忽略 try 块中可能引发的异常,您可以这样做:

while True:
    try:
        if keyboard.is_pressed("q"):
            sys.exit()
    except:
        pass
    ...

【讨论】:

    【解决方案2】:

    尝试删除try

    delay=0.01
    
    while True:
        if keyboard.is_pressed("q"):
            sys.exit()
    def left_mouse():
        while True:
            pyautogui.click()
            pyautogui.delay=delay
    mouse.on_click(left_mouse)
    

    【讨论】:

    • 事实证明删除“try:”是有效的,我所要做的就是修复缩进。我认为这可能会弄乱我的循环
    • 可能还是要删除这个脚本,因为它甚至不起作用:P
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-12-18
    • 1970-01-01
    • 1970-01-01
    • 2021-05-28
    • 2018-02-04
    • 1970-01-01
    • 2022-08-20
    相关资源
    最近更新 更多