[英]Exhaustive Enumeration to find Cube Root (Python)
#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),但是没有任何反应。 我进入“打印ANS”,但它说ANS没有定义。
我已经学习Python 4个小时了。 我希望我只是缺少一些明显的步骤。
是的,问题是我处于交互模式 。 昨天刚开始,我什至不知道区别。 感谢您的快速反应。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.