【问题标题】:Exhaustive Enumeration to find Cube Root (Python)查找立方根的详尽枚举(Python)
【发布时间】: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.

标签: python root cube


【解决方案1】:

是的,问题在于我处于交互模式。 昨天刚开始,我什至不知道有什么区别。 感谢大家的快速回复。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-03-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-08
    相关资源
    最近更新 更多