【发布时间】:2018-02-07 05:35:26
【问题描述】:
#Find the cube root of a perfect cube
x = int(raw_input('Enter an integer: '))
ans = 0
while ans**3 < abs(x):
ans = ans + 1
if ans**3 != abs(x):
print x, 'is not a perfect cube'
else:
if x < 0:
ans = -ans
print 'Cube root of ' + str(x) + ' is ' + str(ans)
当我运行代码时,它会打印“输入一个整数:”。 然后输入一个数字(例如 125),但没有任何反应。 我输入“print ans”,但它说 ans 没有定义。
我已经学习 Python 4 个小时了。 我希望我只是错过了一些明显的步骤。
【问题讨论】:
-
当我运行你的代码时,它工作得很好,你使用的是哪个版本的python?
-
无法复制;这似乎运行良好。听起来您将其输入到交互模式或其他内容中;你可能在那里做错了什么。
-
我建议,你接受你自己的答案,表明问题已经解决。为什么你学习 Python 2 而不是 3? It will not be supported in the near future.