我得到了一段简单的代码,用于添加/减去date /
def calculate(self, datemonthyear, add): #not handled the condition for years between 00 and 09.
year = datemonthyear % 100
datemonthyear = datemonthyear / 100
date = datemonthyear % 100
month = datemonthyear / 100
leapyear = (year % 4 == 0 or year==0)
print month
print date
print year
print ((leapyear and date + add > 29) or (not(leapyear) and date + add > 28))
if (month == 2 and ((leapyear and date + add > 29) or (not(leapyear) and date + add > 28))):
print "1"
return "301" + str(year)
elif month == 3 and leapyear and date + add < 1:
print "2"
return "229" + str(year)
elif month == 3 and not(leapyear) and date + add < 1:
print "3"
return "228" + str(year)
elif month in [1,3,5,7,8,10,12]:
print "4"
if month == 12 and date + add > 31:
return "101" + str((year + 1)%100)
elif month == 1 and date + add < 1:
return "1231" + str((year - 1)%100)
elif month == 8 and date + add < 1:
return "731" + str(year)
elif date + add > 31:
return str(month + 1) + "01" + str(year)
elif date + add < 1:
return str(month - 1) + "30" + str(year)
elif month in [2,4,6,9,11]:
print "5"
if date + add > 30:
return str(month + 1) + "01" + str(year)
elif date + add < 1:
return str (month -1) + "31" + str(year)
print "Has to come here"
return str(month) + str(date + add) + str(year)
当我打电话
print "22820 " + self.calculate(22820, 1)
它会抛出此错误
E 120731 15:27:04 web:1064] Uncaught exception GET / (::1)
HTTPRequest(protocol='http', host='localhost:8881', method='GET', uri='/', version='HTTP/1.1', remote_ip='::1', body='', headers={'Accept-Language': 'en-US,en;q=0.8', 'Accept-Encoding': 'gzip,deflate,sdch', 'Host': 'localhost:8881', 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8', 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_4) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.151 Safari/535.19', 'Accept-Charset': 'ISO-8859-1,utf-8;q=0.7,*;q=0.3', 'Connection': 'keep-alive', 'Cookie': '_xsrf=2328f244ddf94402884f39490aeed347', 'Cache-Control': 'max-age=0'})
Traceback (most recent call last):
File "/Library/Python/2.7/site-packages/tornado-2.3-py2.7.egg/tornado/web.py", line 1021, in _execute
getattr(self, self.request.method.lower())(*args, **kwargs)
File "trends.py", line 33, in get
print "22820 " + self.calculate(22820, 1)
TypeError: cannot concatenate 'str' and 'NoneType' objects
这基本上意味着计算返回None但我无法调试
答案 0 :(得分:0)
快速回答是,当month == 0
错过所有if语句并最终返回函数的默认返回情况时,值为None
。您不能将字符串与None值连接。
答案 1 :(得分:0)
一些评论太长的观察结果:
我只是想确认一下这句话:
print "22820 " + self.calculate(22820, 1)
需要从定义了calculate
的同一个类的方法中调用,并且需要从该类的实例调用调用方法。
not
不是函数,因此not(foo)
没有任何意义 - 除非您拥有自己的名为not
的函数版本,在这种情况下您应该重命名它。