【问题标题】:Why it function works differently in Python compiler and in web.py app?为什么它在 Python 编译器和 web.py 应用程序中的工作方式不同?
【发布时间】:2018-11-23 02:12:26
【问题描述】:

我是一名 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 应用程序中的结果不同呢?谢谢。

【问题讨论】:

  • 你能打印出form01form02form03greeting = ... 行之前的内容吗?也许web.input 并没有达到你的预期。
  • 请注意字符串和整数的区别

标签: python web.py


【解决方案1】:

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

您可以通过在解释器中使用字符串与整数运行脚本来看到这一点。

>>> print myfunc('1980', '3', '4')
('1980 is Ge Sh', 'and its l is somehow', 'the n is CND, the o is CHN', 'The p is Wrong input! and the q is Wrong input!.')

>>> print 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.')

作为旁注,您的代码可以通过使用更具描述性的变量名称来改进,使用 datetime 解析输入并重构以删除您的 if 语句(也许使用字典来映射输入 -> 输出)。

【讨论】:

    【解决方案2】:

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

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

    将获得整数值。其他关于 web.py 传递字符串的 cmets 并不完全正确。 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
    

    正如其他人所建议的那样,将来您应该打印出返回值以实际查看您得到的结果!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多