我正在尝试使用以下代码在 Python 中打印等号 (=
):
num= int(raw_input('Please enter a number: '))
num2= int(raw_input('Please enter another number: '))
print '%s + %s' % (num, num2), = num + num2
我希望输出为:
5 + 10 = 15
但是,我收到以下错误(使用 Python 2.7.8):
File "<stdin>", line 1
print '%s + %s' % (num, num2), = num + num2
^
SyntaxError: invalid syntax
这里有什么问题?
最佳答案
您可以使用格式字符串和 str.format
函数,像这样
print '{} + {} = {}'.format(num, num2, num + num2)
str.format
的参数将在相应的 {}
中被替换。确保参数的数量与 {}
的数量相匹配。
关于python - 打印相等运算符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26374291/