如何搜索多个范围?

时间:2017-09-08 20:58:52

标签: python-3.x

免责声明:我16岁刚刚开始我的A级,计算是我的主题之一。

所以,我有一个挑战,要制定税率计算器。我试图让工资获得正确的税收折扣时遇到问题:

personal_allowance = 11500    
basic_rate = range(11501,45001)    
higher_rate = range(45002,150001)
additional_rate = range(150002,(1 ** 15))

br_tax = float(0.2)
hr_tax = float(0.4)
ad_tax = float(0.45)

问题在于区分数字:

#if not income_amount in basic_rate:
    print("You pay 20% tax. You will pay:\n£"+str("%.2f" % round((income_amount * br_tax),2)))
    income_amount = 0
    time.sleep(0.6)
    print("-------------")
    time.sleep(2)
    Start()

我如何让这条注释线自行排序,我也有更高的薪水,但是所有的都默认为这个块,而不是其他范围的其他块。显然<>不起作用,而not in is组合似乎也无济于事。

作为旁注,由于我的代码不能正确显示,因此双星号是否可用作指数?

additional_rate = range(150002,(1 ** 15))

1 个答案:

答案 0 :(得分:0)

根据我的理解,您需要根据不同的收入水平给出不同的税率?

python中的range函数创建一个序列,例如, range (1,5)1,2,3,4.序列,我认为这是不必要的。

如果您使用if else,您的程序会更容易。

income = 20000 # income of the person

# Rates
br_tax = float(0.2)
hr_tax = float(0.4)
ad_tax = float(0.45)
tax_to_deduct = 0

# Calculate the tax on the amount first

if income>=11501 and income <= 45001:
    tax_to_deduct = br_tax*income

# Then subtract this from your original income.
print ('Tax:',tax_to_deduct)
print ('Income after tax:',income-tax_to_deduct)

对所有括号执行相同操作,使用if else将使您的代码更加直观。我可能误解了你的问题,如果是这样,请告诉我。

是的,双星号用作指数