为什么它的函数在Python编译器和web.py应用程序中的工作方式不同?

时间:2018-06-13 13:12:32

标签: python web.py

我是一名新的Python学习者。这是我在名为test2.py的Python文件中编写的函数,该函数在名为app02.py的Python文件中的基于web.py的应用程序中调用。

# -*- coding: utf-8 -*-

def myfunc(a,b,c):
    d = {1:"US", 2:"CN", 3:"UK", 4:"Di", 5:"Wu", 6:"Ji", 7:"Ge", 8:"Xi", 9:"Re", 10:"Ku"}
    e = {1:"Zi", 2:"Co", 3:"Yii", 4:"Mo", 5:"Ch", 6:"Si", 7:"Wuu", 8:"We", 9:"Sh", 10:"Yoo", 11:"Xu", 12:"Ha"}

    f = int(a) - 3 
    g = (f)%10 
    h = (f)%12 
    a_num = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
    b_num = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
    if g in a_num:
        i = d[g]
    elif g == 0:
        i = d[10]
    if h in b_num:
        j = e[h]
    elif h == 0:
        j = e[12]
    a_text = "%s is %s %s" % (str(a), i, j,)

    k = {1:"Good", 2:"Bad", 3:"Very Good", 4:"Very bad", 5:"Not sure", 6:"sure", 7:"somehow", 8:"what", 9:"You", 10:"Nothing"}

#1
    if i == d[1]:
        l = k[5]
#2
    if i == d[6]:
        l = k[6] 
#3
    if i == d[2]:
        l = k[8]
#4
    if i == d[7]:
        l = k[7]
#5
    if i == d[3]:
        l = k[9]
#6
    if i == d[8]:
        l = k[10]

#7
    if i == d[4]:
        l = k[2]

#8
    if i == d[9]:
        l = k[1]

#9
    if i == d[5]:
        l = k[3]

#10
    if i == d[10]:
        l = k[4]

    b_text = "and its l is %s" % (l)


    m = ("CHN", "GBP", "USD", "CND", "MSN", "WED")

    if j == e[1] or j == e[7]:
        n = m[1]
        o = m[4] 

    elif j == e[2] or j == e[8]:
        n = m[2] 
        o = m[5] 

    elif j == e[3] or j == e[9]:
        n = m[3]
        o = m[0]

    elif j == e[4] or j == e[10]:
        n = m[4]
        o = m[1]

    elif j == e[5] or j == e[11]:
        n = m[5] 
        o = m[2]

    elif j == e[6] or j == e[12]:
        n = m[0]
        o = m[3]

    c_text = "the n is %s, the o is %s" % (n, o)



    c_num = m.index(n)

    if (b == 1 and (c >= 21 and c <= 31)) or (b == 2) or (b == 3 and c <= 21):
        p = m[0]
        q = m[c_num - 2] 

    elif (b == 3 and (c >= 21 and c <= 31)) or (b == 4) or (b == 5 and c <= 21):
        p = m[1]
        q = m[c_num - 1]

    elif (b == 5 and (c >= 21 and c <= 31)) or (b == 6) or (b == 7 and c <= 21):
        p = m[3]
        q = n 

    elif (b == 7 and (c >= 21 and c <= 31)) or (b == 8) or (b == 9 and c <= 21):
        p = m[2]
        q = m[c_num + 1] 

    elif (b == 9 and (c >= 21 and c <= 31)) or (b == 10) or (b == 11 and c <= 21):
        p = m[4]
        q = m[c_num + 2] 


    elif (b == 11 and (c >= 21 and c <= 31)) or (b == 12) or (b == 1 and c <= 21):
        p = m[5]
        q = o 

    else:
        p = "Wrong input!"
        q = "Wrong input!"

    d_text = "The p is %s and the q is %s." % (p, q)

    return a_text, b_text, c_text, d_text

当我在Python编译器中运行该函数时,它运行良好,因为它可以很好地返回d_text。例如:

>>> import test2
>>> test2.myfunc(1980,3,4)
('1980 is Ge Sh', 'and its l is somehow', 'the n is CND, the o is CHN', 'The p is CHN and the q is GBP.')

以下是基于web.py的app02.py,它将调用上面的函数。然后问题来了,因为它无法很好地返回d_text,这会导致“else”结果。但是我希望它能像上面的Python编译器那样做,它会转到“if / elif”结果。

# -*- coding: utf-8 -*-

import web
import test2

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." % ((str(test2.myfunc(form01.a_year, form02.a_month, form03.a_month))))

        return render.index(greeting = greeting)

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

当我尝试在web.py上运行app02.py时输入(1980,3,4)这样:

click to see the picture

然后我得到了web_py返回错误的d_text响应,如下所示:

click to see the response

那么为什么同样的函数在Python编译器和web.py应用程序中有不同的结果?谢谢。

2 个答案:

答案 0 :(得分:0)

web.py将值作为字符串传递,而不是整数。

您可以通过在解释器中使用字符串vs int运行脚本来查看此内容。

datetime

作为旁注,您的代码可以通过使用更多描述性变量名来改进,使用{{1}}来解析输入并重构以删除if语句(可能使用字典来映射输入 - 而不是输出)

答案 1 :(得分:0)

web.input()返回类似dict的结构。您想要一个特定的值:

form01 = web.input(a_day=1).a_day

将获得整数值。关于web.py传递字符串的其他注释并不完全正确。 HTTP(通常)传递字符串值,但`web.input()将根据您初始化的方式转换它们:

a_str =   web.input(a_day='1').a_day
a_int =   web.input(a_day=1).a_day
a_float = web.input(a_day=1.0).a_day

正如其他人所建议的那样,将来你应该打印出你的返回值,以便真正看到你得到的东西!