need help what is the error here - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: need help what is the error here (/thread-9125.html) |
need help what is the error here - m123 - Mar-22-2018 i am trying to find the cube root and print cube=-64 for i in range(abs(cube)+1): if i**3 >=abs(cube): break if i**3 !=abs(cube): print(cube," is not a perfect cube") else: if cube<0: i = -i print('Cube root of ' + str(cube) + ' is ' + str(cube))error is line no 4 1)sure there is a syntax error ?2) here "i" i in range in line no 2 , does it initializes to zero for first iteration.Could you please explain the control how it is passing line by line RE: need help what is the error here - wavic - Mar-22-2018 The 'if' statement must have a code block. Perhaps that 'break' statement? If that is the case, the indentation is wrong. range() starts with 0. In order to start with 1, for example, add it as a second parameter: range(var, 1) .Do you initialize cube with a value of -64? Also, 'i' in line 9? cube=-64 # ? for i in range(abs(cube)+1): if i**3 >=abs(cube): break if i**3 !=abs(cube): print(cube," is not a perfect cube") else: if cube<0: i = -i # ?? print('Cube root of ' + str(cube) + ' is ' + str(cube)) RE: need help what is the error here - m123 - Mar-24-2018 it worked fine got 201 as answer,dont know what is the error but got output ,maybe like you said indentation cube = 8120601 for guess in range(abs(cube)+1): # passed all potential cube roots if guess**3 >= abs(cube): # no need to keep searching break if guess**3 != abs(cube): print(cube, 'is not a perfect cube') else: if cube < 0: guess = -guess print('Cube root of ' + str(cube) + ' is ' + str(guess)) |