Ask your own question, for FREE!
MIT 6.00 Intro Computer Science (OCW) 7 Online
OpenStudy (anonymous):

I keep getting the following error for the code I've written below it. How do I correct for this error? File "C:\Python33\MIT.py", line 4, in while ans**3 < abs(x): TypeError: bad operand type for abs(): 'str' x = input('Enter an integer: ') ans = 0 while ans**3 < abs(x): ans = ans + 1 if ans**3 != abs(x): print('is not a perfect cube') else: if x < 0: ans = -ans print('Cube root of ' + str(x) + ' is ' + str(ans))

OpenStudy (espex):

input() takes all input as a string, you cannot abs() a string, so to fix this, try: x = int(input("Enter an integer: "))

OpenStudy (anonymous):

It worked. Thanks so much! A lot of time wasted trying to correct for such an easy fix. Frustrating.

OpenStudy (espex):

Indeed, the simple problems are usually the most frustrating. :)

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!