简体   繁体   English

input()-行继续符后的意外字符

[英]input() - unexpected character after line continuation character

Ok I've search for this one for quite some time but have not been able to resolve this issue. 确定,我已经搜索了很长时间,但无法解决此问题。 I am sure that this issue lies with input(). 我确定这个问题在于input()。

networkPath = input("Drop Path: ")
print("test") # <- will never get here

SyntaxError: unexpected character after line continuation character SyntaxError:行继续符后的意外字符

Also, if the user puts their input in quotes this error does not occur. 同样,如果用户将其输入用引号引起来,则不会发生此错误。 Such as seen here: syntaxerror: unexpected character after line continuation character in python 如此处所示: 语法错误:python中的行继续字符后的意外字符

I do not want the user to have to wrap their input in quotes. 我不希望用户必须将其输入用引号引起来。 The reasoning is that they will be putting in long network paths such as: \\oursite.com\\someplace\\global\\Communications\\News\\Sitemap 原因是它们将放置较长的网络路径,例如:\\ oursite.com \\ someplace \\ global \\ Communications \\ News \\ Sitemap

Now I know you are thinking, "why can't they just wrap in quotes?" 现在我知道您在想,“为什么他们不能只用引号括起来?” The reasoning is that they would be copying and pasting that network path from a generated email. 原因是他们将从复制的电子邮件中复制并粘贴该网络路径。

Thanks in advance! 提前致谢!

Edit: I should note that this fails in windows command line and not in pyscripter 编辑:我应该注意,这在Windows命令行而不是pyscripter中失败

The root problem here is almost certainly that you're learning Python 3, but trying to use a Python 2.7 interpreter to do it. 这里的根本问题几乎可以肯定是您正在学习Python 3,但是尝试使用Python 2.7解释器来做到这一点。 Don't do that. 不要那样做 Go download Python 3.3 or later and use that. 下载Python 3.3或更高版本并使用它。

But if you're actually intentionally trying to use Python 2.7, read on: 但是,如果您实际上是有意尝试使用Python 2.7,请继续阅读:


I am sure that this issue lies with input(). 我确定这个问题在于input()。

You're right. 你是对的。 As the documentation says, input is: 文档所述input为:

Equivalent to eval(raw_input(prompt)) . 等效于eval(raw_input(prompt))

In other words, whatever the user types in gets passed to eval . 换句话说,无论用户输入什么,都会传递给eval Which means it's being evaluated as Python code. 这意味着它正在被评估为Python代码。 And, unless the user happens to type something that's valid Python code (like a string in quotes), you will get a SyntaxError . 并且,除非用户碰巧键入有效的Python代码(例如带引号的字符串),否则您将收到SyntaxError

The solution here is simple: Don't use input , use raw_input . 这里的解决方案很简单:不要使用input ,而是使用raw_input


(Note that Python 3's input is the same as Python 2's raw_input , and it doesn't have any equivalent to Python 2's input . This is why I believe you're using the wrong Python version.) (请注意,Python 3的input与Python 2的raw_input相同,并且与Python 2的input没有任何等效。这就是为什么我认为您使用错误的Python版本的原因。)

Don't use input() for this; 不要为此使用input(); use raw_input(). 使用raw_input()。 Input() calls eval() on the read-in text, which you neither need or want , and which is responsible for this error. Input()在您不需要或不需要的读入文本上调用eval(),它是造成此错误的原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM