如何添加功能?

时间:2015-05-23 18:31:20

标签: python

如何将从条目中获取值的预定义函数添加到一起?

我想要的是所有这些的总价值,但如果一切都是真的,它只会添加它们。我想要的是即使一个是假的也要添加。

#The Problem
def TotalCost():
    qty1= eVal1()
    qty2= eVal2()
    qty3= eVal3()
    qty4= eVal4()
    qty5= eVal5()
    qty6= eVal6()
    qty7= eVal7()
    qty8= eVal8()
    qty9= eVal9()
    totalsum= qty1+qty2+qty3+qty4+qty5+qty6+qty7+qty8+qty9
    if totalsum >0:
        Total.delete(0, END)
        Total.insert(0, "%.2f" % totalsum)
        return totalsum

从中获取。

功能和定义

value1= IntVar()
value2= IntVar()
value3= IntVar()
value4= IntVar()
value5= IntVar()
value6= IntVar()
value7= IntVar()
value8= IntVar()
value9= IntVar()
value10= IntVar()


#Gets Value from entry and gives total



def eVal1():
    a= value1.get()
    doughnut = "Strawberry" + " X"
    results1 = doughnut + str(a)
    if a>0:
        b= a*0.99
        List1.insert(0, results1)
        return b 


def eVal2():
    c= value2.get()
    doughnut = "Custard" + " X"
    results2 = doughnut + str(c)
    if c>=0:
        d= c*0.99
        List1.insert(0, results2)
        return d


def eVal3():
    e= value4.get()
    doughnut = "Sugar Ring" + " X"
    results3 = doughnut + str(e)
    if e>=0:
        f= e*0.99
        List1.insert(0, results3)
        return f

def eVal4():
    g= value5.get()
    doughnut = "Chocolate Caramel" + " X"
    results4 = doughnut + str(g)
    if g>=0:
        h= g*0.99
        List1.insert(0, results4)
        return h

def eVal5():
    i= value6.get()
    doughnut = "Lemon Circle" + " X"
    results5 = doughnut + str(i)
    if i>=0:
        j= i*0.99
        List1.insert(0, results5)
        return j


def eVal6():
    k= value7.get()
    doughnut = "Blueberry Blaster" + " X"
    results6 = doughnut + str(k)
    if k>=0:
        l= k*0.99
        List1.insert(0, results6)
        return l

def eVal7():
    m= value8.get()
    doughnut = "Strawberry Suprise" + " X"
    results7 = doughnut + str(m)
    if m>=0:
        n= m*0.99
        List1.insert(0, results7)
        return n
def eVal8():
    o= value9.get()
    doughnut = "Simple Sugar" + " X"
    results8 = doughnut + str(o)
    if o>=0:
        p= o*0.99
        List1.insert(0, results8)
        return p

def eVal9():
    q= value10.get()
    doughnut = "Apple Cinnamon" + " X"
    results9 = doughnut + str(q)
    if q>=0:
        r= q*0.99
        List1.insert(0, results9)
        return r

错误消息

第164行,在TotalCost中     totalsum = qty1 + qty2 + qty3 + qty4 + qty5 + qty6 + qty7 + qty8 + qty9 TypeError:+:'NoneType'和'float'

的不支持的操作数类型

1 个答案:

答案 0 :(得分:2)

在所有eVal函数中,您可以在else块中添加return 0条件和else

例如:

def eVal1():
    a= value1.get()
    doughnut = "Strawberry" + " X"
    results1 = doughnut + str(a)
    if a>0:
        b= a*0.99
        List1.insert(0, results1)
        return b 
    else:
        return 0

同样,将其添加到所有eVal函数中。