Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 2.7 python中的运算符_Python 2.7 - Fatal编程技术网

Python 2.7 python中的运算符

Python 2.7 python中的运算符,python-2.7,Python 2.7,我使用的是python版本2.73 这是我的密码 x=int(raw_input('Enter first number\n')) y=int(raw_input('Enter second number\n')) z=x%y print('%d % %d = %d' %(x,y,z)) 现在print语句向我抛出了一个错误。 我希望我的输出看起来像 Enter first number 3 Enter second number 2 3 % 2 = 1 您的变量称为x、y和z,而不是a、b

我使用的是python版本2.73 这是我的密码

x=int(raw_input('Enter first number\n'))
y=int(raw_input('Enter second number\n'))
z=x%y
print('%d % %d = %d' %(x,y,z))
现在print语句向我抛出了一个错误。 我希望我的输出看起来像

Enter first number
3
Enter second number
2
3 % 2 = 1

您的变量称为
x
y
z
,而不是
a
b
c
。此外,还需要通过加倍来转义
%
文本

print('%d %% %d = %d' % (x, y, z))
# Here ----^
# Here ------------------^

必须将单独的百分号加倍,否则python会尝试将下一个字符解释为格式化字符:

'%d %% %d = %d' %(a,b,c)
或者,我建议使用
格式
,因为
%
格式肯定会被弃用(另外,在您的情况下,您不必“转义”百分号:

'{} % {} = {}'.format(a,b,c)

(您的变量称为
x,y,z
,这很容易理解)

谢谢!但您能解释一下为什么我们需要用户双%查看我的编辑。
%
用于分隔格式。Sole
%
后跟空格会触发错误,因为空格不是有效的格式字符。