我尝试使用 %r 作为变量的替代品,并作为此代码中的等式(我可能措辞错误)。
s = int(input("How many sweets do you want to share?:"))
p = int(input("How many people will they be divided between?:"))
print("Each person will have %r sweets." % s/p)
print("There will be %r sweets left over" % s%p)
这是我的错误:
File "pye7.py", line 3, in <module>
print("Each person will have %r sweets.") % s/p
TypeError: unsupported operand type(s) for /: 'str' and 'int'
请您参考如下方法:
它是一种字符串格式化语法。基本用法是将值插入到带有 %s 占位符的字符串中。您正在尝试将表达式插入字符串中,因此您需要将等式括在括号中:
print("Each person will have %r sweets." %(s/p))
