功能未执行

时间:2014-05-04 07:51:13

标签: python function python-3.x

我是Python的新手,所以我在代码中遇到了一些问题。我有一些功能,但tota_additional_bonus()tota_bonus()无效,因为它们没有显示print值。

import sys
def compute(sale,persent):      #function to compute bonus
    return(sale*persent/100)
while (1):
    try:
        print ("Enter Sales Price For Toyata $")
        toyota=int(input())
        print ("Enter Sales Price For Nissan $")
        nissan=int(input())
        print ("Enter Sales Price For Ford $")
        ford=int(input())

        print ("Enter Number Of Sales For Toyata in 2014")
        to=int(input())
        print ("Enter Number of Sales For Nissan in 2014")
        ni=int(input())
        print ("Enter Number Of Sales For Ford in 2014")
        fo=int(input())



    except ValueError:
        print ("Enter An Integer")
        sys.exit()

    sales= ((toyota*to)+(nissan*ni)+(ford*fo))         #calculating total sales of abc



    ct=(toyota*to)             # total sells toyata
    cn=(nissan*ni)             # total sells nissan
    cf=(ford*fo)               # total sells toyata

    print ("Contribution Of Toyota $",ct,"in Total Sales")
    print ("Contribution Of Nissan $",cn,"in Total Sales")
    print ("Contribution Of Ford $",cf,"in Total Sales")
    print ("Total Sales for ABC",sales)

    if sales <= 500000:
        bonus=compute(sales,0.1)

    elif sales >=500001 and sales <=1000000:
        bonus=compute(sales,0.2)+500

    elif sales >=1000001 and sales <=5000000:
        bonus=compute(sales,0.3)+1500

    elif sales >=500001 and sales <=10000000:
        bonus=compute(sales,0.4)+13500
    elif sales >10000000:
        bonus=compute(sales,0.5)+33500

    print ("Total Bonus\n",bonus)       #total bonus

    cty=(ct*bonus)/sales             #bonus contribution toyata
    cny=(cn*bonus)/sales             #bonus contribution nissan
    cfy=(cf*bonus)/sales             #bonus contribution toyata

    print ("Contribution Of Toyota $",cty,"in Total Bonus")
    print ("Contribution Of Nissan $",cny,"in Total Bonus")
    print ("Contribution Of Ford $",cfy,"in Total Bonus")


    print ("Input the Additional bonus for toyota in %")
    x=int(input())
    print ("Input the Additional bonus for Nissan in %")
    y=int(input())
    print ("Input the Additional bonus for Ford in %")
    z=int(input())


    def additionalbonus(bonus,extra):       #function to calculate additional bonus
        return (bonus*extra)
    xn= additionalbonus(bonus,x)
    yn= additionalbonus(bonus,y)
    zn= additionalbonus(bonus,z)


    print("Additional Bonus for toyota",xn)
    print("Additional Bonus for nissan",yn)
    print("Additional Bonus for ford",zn)


    def tota_additional_bonus(xn,yn,zn):       #calculating total additional bonus 
        t_n = xn +yn + zn
        print ("Total Adiitional Bonus is",t_n)

    def tota_bonus(bonus,t_n):                 #calculating total bonus distributed by ABC
        bonus_n=bonus+t_n
        print ("Total Bonus distributed is",bonus_n)

    print ("Enter Y to Calculate for another Year or N to Exit")
    var= input()

    if var == 'y':
        pass
    elif var == 'n':
        exit()


input()

1 个答案:

答案 0 :(得分:1)

在主while循环结束时:

print("Additional Bonus for ford",zn)


def tota_additional_bonus(xn,yn,zn):       #calculating total additional bonus 
    t_n = xn +yn + zn
    print ("Total Adiitional Bonus is",t_n)

def tota_bonus(bonus,t_n):                 #calculating total bonus distributed by ABC
    bonus_n=bonus+t_n
    print ("Total Bonus distributed is",bonus_n)

print ("Enter Y to Calculate for another Year or N to Exit")

定义两个函数print它们的参数总和,但从不实际调用它们。此外,似乎第二个函数需要一个在第一个函数中计算但未返回的值。

相反,请尝试:

print("Additional Bonus for ford",zn)


def tota_additional_bonus(xn,yn,zn): # define function 
    t_n = xn +yn + zn
    print ("Total Adiitional Bonus is",t_n)
    return t_n # return result

t_n = tota_additional_bonus(xn, yn, zn) # call function and assign result

def tota_bonus(bonus,t_n): # define function 
    bonus_n=bonus+t_n
    print ("Total Bonus distributed is",bonus_n)

tota_bonus(bonus, t_n) # call function 

print ("Enter Y to Calculate for another Year or N to Exit")