Hello, new to python a bit. I get an indentation error at line 10, "m -= 1". Sorry if this is a repeated post, I've tried searching but found no solution.

# calculates sum of squares of natural numbers
print "Enter number of terms."
n = raw.input("Terms? ")

first_term = ( ( n * n + n ) / 2 ) - 1

m = n
if m != 0:
    b = m * m + m
    m -= 1
    a = b

while m! = 0:
    a = b
    b = m * m + m
    m -= 1
    a = a + b
else:
    break

second_term = a

third_term = 1

answer = first_term + second_term + third_term

print answer

Recommended Answers

All 4 Replies

EDIT: Never mind. Problem solved.

Hello, new to python a bit. I get an indentation error at line 10, "m -= 1". Sorry if this is a repeated post, I've tried searching but found no solution.

# calculates sum of squares of natural numbers
print "Enter number of terms."
n = raw.input("Terms? ")

first_term = ( ( n * n + n ) / 2 ) - 1

m = n
if m != 0:
    b = m * m + m
    m -= 1
    a = b

while m! = 0:
    a = b
    b = m * m + m
    m -= 1
    a = a + b
else:
    break

second_term = a

third_term = 1

answer = first_term + second_term + third_term

print answer
commented: solution? -3

There are a number of other errors, wit this:

# calculates sum of squares of natural numbers
print "Enter number of terms."
n = int(raw_input("Terms? "))

first_term = ( ( n * n + n ) / 2 ) - 1

m = n
if m != 0:
    b = m * m + m
    m -= 1
    a = b

while m != 0:   # use !=
    a = b
    b = m * m + m
    m -= 1
    a = a + b
else:
    print "----"  # test
    #break  # error: at that point you are outside of the loop

second_term = a

third_term = 1

answer = first_term + second_term + third_term

print answer

You should also get errors for

n = raw.input("Terms? ")

and for

b = m * m + m
first_term = ( ( n * n + n ) / 2 ) - 1

and the same as the above comment

if m != 0:   

will always be true the way the program is coded (hint: raw_input returns a string)

Start with one of the tutorials It is well worth the time spent. BTW, indentation errors that aren't obvious may be the mixing of tabs and spaces when indenting, and tabs can be given different values depending on the settings in your window manager. The "rule" is to only use spaces.

This wasn't an indentation error. Your program has a lot of Syntac errors. On line,
n = raw.input(Term?) This was the first error that dot shouldn't be there
rather it should be n = raw_input("Term?"). and again the n = raw_input recieves
a string not an integer, once again when recieving integer use input.

Rember this The break statement should be Inside a while Loop, Never should it be out side a while Loop

Here is the more enhanced version

print "Number of terms."
n = raw_input("Terms?")
n = int(n) #almost the same as n = input("Terms?")
#and again since you are getting a number from the user
#you should consider using input or if you use raw_input convert it to
#the integer
first_term = ( ( n * n + n ) / 2 ) - 1

m = n
if m != 0:
    b = m * m + m
    m -= 1
    a = b

while m != 0:
    a = b #I guess you are repeating this
    b = m * m + m
    m -= 1
    a = a + b
else:
    # You need to put in mind that this else cluse isn ot in a loop
    #the appropriate place for this break is to be on while loop
    #break
    print "Done"

second_term = a # another error is here a is not 

third_term = 1

answer = first_term + second_term + third_term

print answer
n = int(n) #almost the same as n = input("Terms?")
#and again since you are getting a number from the user
#you should consider using input or if you use raw_input convert it to
#the integer
first_term = ( ( n * n + n ) / 2 ) - 1

m = n
if m != 0:
    b = m * m + m
    m -= 1
    a = b

while m != 0:
    a = b #I guess you are repeating this
    b = m * m + m
    m -= 1
    a = a + b
else:
    # You need to put in mind that this else cluse isn ot in a loop
    #the appropriate place for this break is to be on while loop
    #break
    print "Done"

second_term = a # another error is here a is not 

third_term = 1

answer = first_term + second_term + third_term

print answer
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.