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))
Still Need Help?
Join the QuestionCove community and study together with friends!
Sign Up
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. :)