input() - unexpected character after line continua

2019-03-01 12:53发布

问题:

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().

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

SyntaxError: unexpected character after line continuation character

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

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

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

回答1:

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. Don't do that. Go download Python 3.3 or later and use that.

But if you're actually intentionally trying to use Python 2.7, read on:


I am sure that this issue lies with input().

You're right. As the documentation says, input is:

Equivalent to eval(raw_input(prompt)).

In other words, whatever the user types in gets passed to eval. Which means it's being evaluated as Python code. And, unless the user happens to type something that's valid Python code (like a string in quotes), you will get a SyntaxError.

The solution here is simple: Don't use input, use 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.)



回答2:

Don't use input() for this; use raw_input(). Input() calls eval() on the read-in text, which you neither need or want , and which is responsible for this error.