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

运行first_ai.py脚本出现IndentationError缩进错误的技术求助

Fixing the IndentationError and Syntax Issue in Your Python Script

Let’s break down the problems you’re facing and fix them step by step:

1. What’s Causing the IndentationError?

Python uses strict, consistent indentation (either spaces or tabs—never a mix) to define code blocks. The error IndentationError: unindent does not match any outer indentation level means your line NN = Neural_Network() has an indentation that doesn’t align with the code block it’s supposed to belong to. This usually happens when:

  • You’ve mixed tabs and spaces for indentation
  • The line has more/fewer indentation spaces than the surrounding code in its block

2. Fix the Indentation

First, make sure your entire script uses the same indentation style (4 spaces is the Python community standard). Then adjust the indentation of NN = Neural_Network() to match the outer code block it’s part of.

For example, if this code is at the top level of your script (not inside a function/conditional), it should look like this:

NN = Neural_Network()
o = NN.forward(X)
print("Sorti predit par l'IA : \n" + str(o))
# Fix the missing + operator here (we'll cover this next)
print("Vrai sorti : \n" + str(y))

If it’s inside a code block like an if statement or function, ensure all lines in the block are indented uniformly:

def run_prediction():
    NN = Neural_Network()
    o = NN.forward(X)
    print("Sorti predit par l'IA : \n" + str(o))
    print("Vrai sorti : \n" + str(y))

3. Bonus: Fix the Hidden Syntax Error

You also have a small syntax mistake in your last print statement—you’re missing a + between the string and str(y). This would throw an error once you fix the indentation, so we’ve corrected it in the examples above.

Quick Tips to Avoid Indentation Headaches

  • Turn on "show whitespace" in your code editor (VS Code, PyCharm, etc.) to see exactly what indentation characters you’re using
  • Set your editor to automatically convert tabs to 4 spaces
  • Double-check that all lines in the same code block have identical indentation levels

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.14 06:50:15