运行first_ai.py脚本出现IndentationError缩进错误的技术求助
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

