【问题标题】:Python program via command prompt not allowing > or < as inputs通过命令提示符的 Python 程序不允许 > 或 < 作为输入
【发布时间】:2019-04-18 05:11:33
【问题描述】:

我正在尝试将包含 > 、

user_input=input("Input data here")

输入:

2>

但是,当我通过命令提示符运行它时,我得到了这个

File "<string>", line 1
2>
 ^
SyntaxError: unexpected EOF while parsing

我可以通过在 jupyter 控制台或空闲运行代码来使其正常工作...我什至可以通过输入 "2&gt;" 使其正常工作,但我不能指望我的用户在任何情况下运行它除了命令提示符或放在引号中。

这里有什么可能的解决方案吗?

【问题讨论】:

  • 我怀疑它是从 cmd 在 python 2 中运行的。从命令窗口运行时查看raw_input 是否有效
  • 无法在 windows cmd 中使用 python 3.x 重新创建它
  • @roganjosh Yep 能够通过从 cmd 运行 python 2.x 来重新创建它
  • 啊! @roganjosh 你是对的!谢谢!

标签: windows python-2.7 cmd


【解决方案1】:

我的错误... Python 2.7 显然已设置为运行文件的默认值。
通过更改python路径解决:

ftype Python.File=C:\Path\to\python.exe "%1" %*

否则,如果我想使用 Python 2.7,我可以使用

来解决这个问题
user_input=raw_input("insert data here")

【讨论】:

    【解决方案2】:

    input 函数用于在 Python 命令行程序 中从用户那里获取数据。您可以使用raw_input,它以字符串的形式从标准输入中获取输入,并立即从一行中读取数据。

    user_input = raw_input('Input data here')
    

    【讨论】:

      猜你喜欢
      • 2016-10-12
      • 2013-02-08
      • 2022-01-10
      • 2018-12-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多