我的if elif和'and'返回无类型

时间:2016-08-26 08:18:46

标签: python-3.x

def taxcalc(gsalary2):
   tax=0
   if(gsalary2 <= 10164):
      tax = 0
    elif gsalary2 > 10164 and gsalary2 <= 19740 :
      tax1 = 10 / 100 * 10164
      tax2 = (15 / 100 * (gsalary2 - 10164))
      tax = tax1 + tax2
    elif gsalary2 > 19740 and gsalary2 <= 29316 :
      tax1 = 10 / 100 * 10164
      tax2 = 15 / 100 * 9576
      tax3 = (20 / 100 * (gsalary2 - 19740))
      tax = tax1 + tax2 + tax3
    elif (gsalary2 > 29316 and gsalary2 <= 38892) :
      tax1 = 10 / 100 * 10164
      tax2 = 15 / 100 * 9576
      tax3 = 20 / 100 * 9576
      tax4 = 25 / 100 * (gsalary2 - 29316)
      tax = tax1 + tax2 + tax3 + tax4
    elif gsalary2 > 38892 :
      tax1 = 10 / 100 * 10164
      tax2 = 15 / 100 * 9576
      tax3 = 20 / 100 * 9576
      tax4 = 25 / 100 * 9576
      tax5 = 30 / 100 * (gsalary2 - 38892)
      tax = tax1 + tax2 + tax3 + tax4 + tax5
    return tax

上述功能完全失败 只有这一部分似乎有用

elif gsalary2 > 38892 :
      tax1 = 10 / 100 * 10164
      tax2 = 15 / 100 * 9576
      tax3 = 20 / 100 * 9576
      tax4 = 25 / 100 * 9576
      tax5 = 30 / 100 * (gsalary2 - 38892)
      tax = tax1 + tax2 + tax3 + tax4 + tax5
      return tax

1 个答案:

答案 0 :(得分:1)

确保您的return tax行与if ...的缩进级别相同。你现在拥有它的方式,如果你的最后elif为真,它看起来只会返回一个值。您收到None(而不是0),因为您的函数没有返回任何内容,因此返回默认的None。换句话说,您需要确保始终返回tax

def taxcalc(gsalary2):
    tax = 0
    if(gsalary2 <= 10164):
    ...
    return tax