preamble
In our initial learning of pyton, due to insufficient mastery of the python language, it will lead to the preparation of the code, run-time errors. For example, it is easy to report an error unexpected indent problem, the following examples to illustrate the problem.
1. Example (correct code)
We want to define a Fiboracci series function by writing a subfunction.
The correct code should be as follows
# Define a Fiboracci series function def fib(n): # write Fibonacci series up to n """Print a Fibonacci series up to n.""" a, b = 0, 1 while a < n: print(a, end=' ') a, b = b, a+b print()
The following screenshot of the code written in IDLE
Based on the above code, the results of the run are as follows, and as you can see from the run results in the figure below, no errors were reported.
Try again to calculate the Fibonacci series up to 23, enter fib(23) in the IDLE shell, and run the result as follows:
2. Example (error with unexpected indent)
If we do not pay attention to the indentation of each line, it is easy to occur unexpected indent (accidental indentation), such as the following error code example
In this code above a is indented by one more space, and it runs with an unexpected indent error.
In the code above a is indented two spaces too much, and an unexpected indent error occurs at runtime.
In the code above, a is indented by 10 spaces, and the same unexpected indent error occurs at runtime.
3. Solutions and summaries
Solution: Strictly control the number of indented spaces.
Based on the above example test, we can see that in python on the code indentation has strict requirements, otherwise it is easy to run the problem of reporting errors, and prompt "unexpected indent" (that is, "unexpected indentation"), therefore Therefore, when you learn, you must strictly control the indentation of the code, to prevent running errors, affecting the progress of learning or work.
to this article on the python error unexpected indent solution to this article, more related python error unexpected indent content please search for my previous posts or continue to browse the following related articles I hope you will support me in the future more!