1

我得到了一段简单的代码来玩弄日期的加法/减法/

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 但我无法调试它

4

2 回答 2

0

一些意见太长,无法发表评论:

我只是想确认一下这个说法:

print "22820 " + self.calculate(22820, 1)

需要从您calculate定义的同一类的方法中调用,并且调用方法需要从该类的实例中调用。

not不是函数,所以not(foo)没有任何意义——除非你有自己的函数版本not,在这种情况下你应该重命名它。

于 2012-08-01T00:48:07.187 回答
0

快速回答是,当month == 0您错过所有 if 语句并最终以 is value 的函数返回默认情况时None。您不能将字符串与 None 值连接。

于 2012-08-01T00:25:59.990 回答