Python indentation distinction analysis

  • 2020-04-02 13:27:32
  • OfStack

Take a closer look at the next two python programs. The code is identical, but the result is different, because the return indentation on the last line is different


def powersum(power, *args):
'''Return the sum of each argument raised to specified power.'''
total = 0
for i in args:
total += pow(i, power)
return total

Powersum (2,3,4) output 25 (3 squared plus 4 squared)

def powersum(power, *args):
'''Return the sum of each argument raised to specified power.'''
total = 0
for i in args:
total += pow(i, power)
return total

Powersum (2,3,4) and 9 (3 squared)

Thus, when writing python code, you cannot indent it arbitrarily


Related articles: