You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python while True循环内代码缩进错误问题排查

代码错误定位

你的程序死循环的核心原因是代码存在缩进错误和语法错误,每次启动都会直接抛出异常,被外层except捕获后触发continue回到循环开头,永远无法执行到退出循环的break语句。具体错误点如下:

  • 核心缩进错误:if video_or_image == "image"对应的视频处理else分支缩进错位。你在写完图片分支的audiovizualizere调用后,插入了一行8空格缩进的注释# Video section good,这个缩进层级和最外层if len(video_tags) == 1同级,相当于提前终止了外层else(即len(video_tags)!=1分支)的代码块,导致后面12空格缩进的else:找不到匹配的同级if,直接抛出语法错误。
  • 连带逻辑问题:视频处理的else块内只有注释,没有可执行代码,就算修正缩进,随机选到视频分支时也会直接跳过业务逻辑走到break,不符合业务预期。
  • 语法错误(直接触发异常):pixabay图片分支的最后一行img_edit调用中,最后一个路径参数"prep_image.png缺少闭合双引号,代码运行到该分支时会直接抛出语法错误。
修正后参考代码

(仅修正缩进、语法错误,原有业务逻辑未改动,视频分支预留位置补全你的代码即可)

while True:
    try: 
        # video tas can be used for an image 
        if len(video_tags) == 1:
            #That meaan that a code1 or code2 function will be activated 
            # I deleted a code1 function because a gifs was in the bad quality 
            code2_function()
            print("Function code2 has been activated")
            check_px("code_img.png")
            image_resize("code_img.png",1920,1080,"res_image.png")
            if get_rgb("res_image.png",1730,11) > 700:
                print("white")
                img_edit("res_image.png","QR_white.png",1580,20,"prep_image.png")
            else:
                print("black")
                img_edit("res_image.png","QR_black.png",1580,20,"prep_image.png")
            audiovizualizere("output.mp3","prep_image.png")
            #Everithink is done
            
        else:
            print(video_tag)
            video_or_image = random.choice(["image","video"])
            print(f"This a random picker to video_or_image:  {video_or_image}")
            if video_or_image == "image":
                pexels_or_pixabay_image = random.choice(["pexels","pixabay"])
                print(pexels_or_pixabay_image)
                if pexels_or_pixabay_image == "pexels":
                    print("A image to video from pexels has been activated!")
                    image_to_video_from_pexels(video_tag,"image_from_pexels.png")
                    check_px("img_from_pexels")
                    image_resize("img_from_pexels.png",1920,1080,"res_image.png")
                    if get_rgb("res_image.png",1730,11) > 700:
                        print("white")
                        img_edit("res_image.png","QR_white.png",1580,20,"prep_image.png")
                    else:
                        print("black")
                        img_edit("res_image.png","QR_black.png",1580,20,"prep_image.png")
                else:
                    # this is only temporary
                    print("A image to video from pexels has been activated!")
                    image_to_video_from_pexels(video_tag,"image_from_pexels.png")
                    check_px("img_from_pexels")
                    image_resize("img_from_pexels.png",1920,1080,"res_image.png")
                    if get_rgb("res_image.png",1730,11) > 700:
                        print("white")
                        img_edit("res_image.png","QR_white.png",1580,20,"prep_image.png")
                    else:
                        print("black")
                        # 补全缺失的闭合双引号
                        img_edit("res_image.png","QR_black.png",1580,20,"prep_image.png")
                audiovizualizere("output.mp3","prep_image.png")    
            # 调整注释缩进至与上层if同级,修复缩进断裂问题
            # Video section good
            else:
                # 在此处补全视频处理业务代码即可
                pass
        # 所有业务逻辑执行完成后才会触发break退出循环
        break
    except Exception:
        # 建议调试时打开以下注释打印异常信息,不要静默吞错
        # import traceback
        # traceback.print_exc()
        continue

额外提示:不建议用空except直接捕获所有异常后无脑重试,至少要打印异常日志方便定位问题,否则后续出现业务错误时还是会陷入无感知的死循环。

内容的提问来源于stack exchange,提问作者TheSoftProgrammer

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.28 17:39:22