What is reason for syntax error of print statement of given python code [closed]

流过昼夜 提交于 2019-12-02 23:52:02

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!