- 不支持的操作数类型:'Storage' 和 'int'

Unsupported operand type(s) for -: 'Storage' and 'int'

我正在学习 Python 2.7 并尝试在名为 new5.py 的模块中编写一个函数,如下所示:

def compare(a,b,c):
    if a - 3 == 8:
        return "I like a!"
    elif b == c:
        return "I like c!"
    else:
        return "I like b!"

当我尝试调用名为 app02.py 的模块中的函数时,在问题的末尾显示了详细代码,我被告知是这样的在屏幕截图中显示如下:

我猜问题出在a,但是我应该怎么做才能使用这个功能呢?谢谢!

------ 以下是根植于 web.py 0.3 的模块 app02.py ------

import web
import new5

urls = (
    '/dyear', 'Index'
)

app = web.application(urls, globals())

render = web.template.render('/Users/Administrator/projects/gothonweb/templates/', base="layout01")


class Index(object):
    def GET(self):
        return render.hello_form01()

    def POST(self):
        form01 = web.input(a_year=1980)
        form02 = web.input(a_month=01)
        form03 = web.input(a_day=01)

        greeting = "Your result from app02 is %s" % (new5.compare(form01, form02, form03))
        return render.index(greeting = greeting)

if __name__ == "__main__":
    app.run()

与其将整个 Storage 对象传递给 compare(),不如访问 form01 中的 a_year、form02 中的 a_month 和 form03 中的 a_day 对象,例如 form01.a_yearform2.a_monthform3.a_day 所以你的函数调用应该看起来像

greeting = "Your result from app02 is %s" % (new5.compare(form01.a_year, form02.a_month, form03.a_day))

此外,请注意 docs

Note that the web.input() values will be strings even if there are numbers passed to it.

所以你需要将你的 web.input 从字符串类型转换为所需的类型(这里是 int),就像这样

if int(a) - 3 == 8: