Understanding Python Print Errors

Asked 2 years ago, Updated 2 years ago, 41 views

How to install tesseract and PyOCR on Windows 10 without using pipRefer to the site
If you run the program below, you will get an error.Why is that?

Error Contents

File"<ipython-input-15-8474a031 face>", line 4
    print("No OCR tool found")
        ^
IndentationError: expected an indented block

program statement

tools=pyocr.get_available_tools()
iflen(tools) == 0:
print("No OCR tool found")
sys.exit(1)

python python3

2022-09-30 21:44

3 Answers

First point
If you copy the linked source exactly as it is, print and sys.exit are preceded by full-width spaces, respectively.

Second point
Indentation is very important in Python.Find out what the program is doing.

"If there is no result, print the characters and exit the program." So each instruction is
Must be indented

 iflen(tools) == 0:
  print("No OCR tool found")
  sys.exit(1)

Third
The link is divided into paragraphs for explanation, but as far as the error message goes, the source you ran does not have enough module import descriptions.The correct answer should be as follows:

 from PIL import Image
import sys
import pyocr
import pyocr.builders

tools=pyocr.get_available_tools() 

iflen(tools) == 0:
  print("No OCR tool found")
  sys.exit(1)


2022-09-30 21:44

tools=pyocr.get_available_tools()
iflen(tools) == 0:
  print("No OCR tool found")#two space before code
sys.exit(1)


2022-09-30 21:44

IndentationError: expected an indented block

If you translate this error into Japanese, it says, "Indentation error: indented block should be in here."

The following code should prevent this error.

tools=pyocr.get_available_tools()
iflen(tools) == 0:
    print("No OCR tool found")
    sys.exit(1)

In this program, if len(tools)==0 is true, run the programs print("No OCR tool found") and sys.exit(1).If it doesn't work, I won't do anything.

Python thus expresses what to do if the conditions of the if statement are met by placing a blank (indent) at the beginning of the line.


2022-09-30 21:44

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.