Python 2.7 python中的运算符
我使用的是python版本2.73 这是我的密码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
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%
后跟空格会触发错误,因为空格不是有效的格式字符。