Edit the Python program loop in Pycharm, the cause of the IF statement error (format issue) __python

Source: Internet
Author: User

In Pycharm, when editing a program, often the code is correct, but do not know where the error, here in the following example to illustrate the importance of code format in the Pycharm.

Title: Write a function, accept a positive even number as a parameter, output 2 prime numbers, and the sum of these 2 primes equals the original positive even, there are many groups of eligible primes, then all output.

Import Math
def isprime (n):
    m=int (MATH.SQRT (n)) +1 for
    i in  range (2,m):
        if n%i==0:
            return  False return
        True

def judge (N):
    if Isinstance (n,int) and n>0 and n%2==0:
 for I in range (3, int (n /2) +1):
     if I%2==1 and IsPrime (i) and IsPrime (n-i):
               print (i, ' + ', n-i, ' = ', n)

judge (80)


Results The error is shown below;

D:\learn\python\python.exe d:/learn/code/myfirstpython.py
File "d:/learn/code/myfirstpython.py", line 11
For I in range (3, int (N/2) +1):
^
Indentationerror:unindent does not match no outer indentation level

Process finished with exit code 1


After modification, move the For statement in the judge function to the previous if statement. This is because the for statement is to be processed by the IF statement, noting ":" after the IF statement.

As shown in the following illustration:

Import Math
def isprime (n):
    m=int (MATH.SQRT (n)) +1 for
    i in  range (2,m):
        if n%i==0:
            return  False return
        True

def judge (N):
    if Isinstance (n,int) and n>0 and n%2==0:
      for I in range (3, int (n /2) +1):
       if I%2==1 and IsPrime (i) and IsPrime (n-i):
               print (i, ' + ', n-i, ' = ', n)

judge (80)

D:\learn\python\python.exe d:/learn/code/myfirstpython.py
5 + 75 = 80
7 + 73 = 80
9 + 71 = 80
11 + 69 = 80
13 + 67 = 80
15 + 65 = 80
17 + 63 = 80
19 + 61 = 80
21 + 59 = 80
23 + 57 = 80
25 + 55 = 80
27 + 53 = 80
29 + 51 = 80
31 + 49 = 80
33 + 47 = 80
35 + 45 = 80
37 + 43 = 80
39 + 41 = 80

Process finished with exit code 0


This prompts us to use pycharm in the future to write the format problem. Details determine success or failure.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.