【问题标题】:Input problems in cmdcmd输入问题
【发布时间】:2016-07-21 23:18:37
【问题描述】:

我遇到了一个问题,如果我在 Windows 终端中运行我的 python 程序,带有插入变量的文本 (%s) 会产生古怪的结果,而在 python shell 中它可以正常工作。

代码:

print("Hi! What's your name?")
name = input("name: ")
print("Nice to meet you %s" % name)
print("%s is a good name." % name)
print("This line is only to test %s in the middle of the text." % name)
input("press enter to exit")

python shell 中的结果:

cmd 中的结果:

我使用的是 Windows 10 和 python32,以防你需要知道。

【问题讨论】:

  • 这很有趣,请您使用format 代替% 并发布结果吗?
  • 在 Win8.1 上对我来说很好。我从 Windows 命令行运行它,输出看起来就像你的 Python shell 结果。
  • 在 Windows 7 下为我工作

标签: python shell python-3.x input cmd


【解决方案1】:

这是 Windows 上原始 3.2.0 中的一个错误。 input() 语句去除了“\n”,但没有去除“\r”,因此字符串输入被隐藏。

https://bugs.python.org/issue11272

快速修复:

name = input("name: ").rstrip()

它已在 3.2.1 中修复。你真的应该升级你的 Python!

【讨论】:

    猜你喜欢
    • 2021-08-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-27
    • 2012-06-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多