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

Python代码出现IndentationError: expected an indented block错误求助

修复Python质数判断代码的缩进错误

你的代码报错是因为Python对缩进有严格要求,所有代码块(比如for循环、if语句内部的代码)必须统一缩进(通常用4个空格)。原代码里for循环下的if语句、if下的执行代码都没有正确缩进,导致解释器无法识别代码结构。

修正后的完整代码:

x = int(input("Enter a number : "))
if x > 1:
    for f in range(2, x):
        if (x % f) == 0:
            print(x, "is not a prime number ")
            print(f, "times", x // f, "is", x)
            break
    else:
        print(x, "is a prime number ")
else:
    print(x, "is not a prime ")

缩进说明:

  • for循环语句下的if代码块需缩进4个空格
  • if判断后的两行print和break语句再缩进4个空格
  • 与for配对的else语句要和for保持同一缩进层级,其下的print同样缩进4个空格

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.05 19:05:27