所以我在使用这段代码时遇到了很多麻烦......它给了我一个错误,我没有看到它有什么问题!我有大约15天的时间学习python,但我的代码中并没有看到错误
当我尝试在python shell中运行它时,它给了我这个错误:
"unsupported operand type(s) for %: 'NoneType' and 'str'"
请帮帮我T____T我很沮丧!
@classmethod
def populationCount(cls):
print ("The total number of humans is: %d.") %(cls.population)
答案 0 :(得分:2)
你在这一行中遇到问题的原因是:
print ("The total number of humans is: %d.") %(cls.population)
首先执行print
,将第一组括号中的内容作为参数。然后,print
的返回值将与%
运算符一起使用。 print
返回None
,这就是Python尝试做的事情:
None % (cls.population)
你可以找出为什么 无法工作。
要解决此问题,请将整个%
操作放在括号中,以便在调用 print
之前完成。毕竟,您希望打印插值的结果。
print("The total number of humans is: %d." % cls.population)
答案 1 :(得分:0)
如果替换
怎么办?print ("The total number of humans is: %d.") %(cls.population)
带
print ("The total number of humans is: %d." % cls.population)
在您的打印声明中?
注意:在提问时,请尝试将代码示例修剪为有问题的部分。它使人们更容易为您提供帮助,事实上,在某些情况下,它甚至可以帮助您找到问题。
答案 2 :(得分:0)
在Python中,除非在代码中另有说明,否则括号只会更改操作的顺序:
print ("%s *remains silent*" % (self.name,)) # comma indicates tuple, not change of order