Python 3 中的 \`raw_input()\` 和 \`input()\` 有什么区别?

新手上路,请多包涵

raw_input()input() 在 Python 3 中有什么区别?

原文由 pkumar 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 474
2 个回答

区别在于 raw_input() 在 Python 3.x 中不存在,而 input() 存在。 Actually, the old raw_input() has been renamed to input() , and the old input() is gone, but can easily be simulated by using eval(input()) . (请记住 eval() 是邪恶的。如果可能,请尝试使用更安全的方法来解析您的输入。)

原文由 Sven Marnach 发布,翻译遵循 CC BY-SA 4.0 许可协议

在 Python 2 中, raw_input() 返回一个字符串,而 input() 尝试将输入作为 Python 表达式运行。

由于获取字符串几乎总是您想要的,因此 Python 3 使用 input() 。正如 Sven 所说,如果您想要旧的行为, eval(input()) 有效。

原文由 Thomas K 发布,翻译遵循 CC BY-SA 2.5 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题