问题
xString = input("Enter a number: ")
x = int(xString)
yString = input("Enter a second number: ")
y = int(yString)
print('The sum of ', x, ' and ', y, ' is ', x+y, '.', sep='')
on executing above code, interpretor throwing syntax error saying syntax error as below.
print(?The sum of ?, x, ? and ?, y, ? is ?, sum, ?.?, sep=??)
SyntaxError: invalid syntax
回答1:
The single quote used in the print statement is ' with ascii value 39.
>>> ord("'")
39
The one ’ used in the print statement in the question is not a quote ' but RIGHT SINGLE QUOTATION MARK' (U+2019)
>>> u"’"
u'\u2019'
Since, you are using python 2, to use sep in the print statement you need to import the functionality from the future.
from __future__ import print_function
print('The sum of ', x, ' and ', y, ' is ', x+y, '.', sep='')
回答2:
It's because of those wacky quote characters like ’. Change them to ' characters and you should not have any problem.
回答3:
First, replace the ’ by '
Second, you may need to add one more sentence: from __future__ import print_function
来源:https://stackoverflow.com/questions/37475271/what-is-reason-for-syntax-error-of-print-statement-of-given-python-code