Elif和range函数,用于按百分比累进计算征税 [英] Elif and range functions for progressive taxation calculation in percentage

查看:68
本文介绍了Elif和range函数,用于按百分比累进计算征税的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个程序,其中用户将年收入作为输入,并且所得税率将根据百分比阈值进行缩放(有关收入范围与百分比阈值的信息,请参见此文章末尾的图片).例如,从100,000欧元的收入中,超过60001的收入将支付50%(如下表所示).在45000-60000之间为40%,在15000-30000之间为20%,在0-10000之间为10%.方程应该是0.5 * 40 000 + 0.4 * 150 00 + 0.3 * 15000 + 0.2 * 15000 + 0.1 * 10000 = 35000.

I am creating a program where user puts yearly income as an input and the income taxation rate would scale according to percentage thresholds (see picture at the end of this post for income range vs. percentage thresholds). For example from 100,000 euro income 50% would be paid from income exceeding 60001 (as according in the table below). 40% betweeen 45000-60000, 20% between 15000-30000 and 10% between 0-10000. Equation should go something like 0.5* 40 000 + 0.4 * 150 00 + 0.3 * 15 000 + 0.2 * 15 000 + 0.1 * 10000 = 35 000.

输出应类似于:给出您的年收入:100000缴税35000.0欧元.您的税率是:35.0%.给出您的年收入:54000缴税12600.0欧元.您的纳税百分比为:23.333333333333332%.

Outputs should be something like: Give your yearly income: 100000 You pay taxes 35000.0 euro. Your taxation percent is: 35.0 %. Give your yearly income: 54000 You pay taxes 12600.0 euroa. Your taxation percent is: 23.333333333333332 %.

这是我尝试过的:

yearly = int(input("Tell your yearly income:"))
one = 0.1
two = 0.2
three = 0.3
four = 0.4
five = 0.5 
if yearly in range(1,15000):
    print("You pay taxes in total:",yearly*one)
if yearly in range(15001,30000):
    print("You pay taxes in total:",yearly*two)
if yearly in range(30001,45000):
    print("You pay taxes in total:",yearly*three)
if yearly in range(45001,60000):
    print("You pay taxes in total:",yearly*four)
if yearly > 60000:
    print("You pay taxes in total:",yearly*five)

征税百分比不会像我希望的那样扩展.我认为这应该通过elif和range函数来完成.百分比的最小值和最大值可能会有所帮助,但我不知道如何将输出转换为百分比.请注意,我这里可能会有一些理解问题,数学不是我最擅长的游戏,但是我尽了最大的努力来描述这个问题...

The taxation percent doesn't scale like I'd like it to. I assumption is that this should get done by elif and range functions. Maybe min and max values of percentages would help, but I don't know how to turn outputs into percentages. Please note that I might have some comprehension problems here, math is not my strongest gamem but I did my absolute best to describe the problem...

这里是收入计数与所用税收百分比的表.收入与税收百分比

Here is a table of income count vs. taxation percentage used. Income vs. taxation percentage

推荐答案

优化方式:

这是一种更优化的方法(请参阅底部的枚举非优化方法).我将百分比和阈值存储在列表中,并且 taxes 在循环中递增:

Here is a more-optimized way to do it (see at bottom for a non-optimized way with enumeration). I store the percents and thresholds in lists, and taxes is incremented during a loop :

yearly = int(input("Tell your yearly income:"))

percents = [0.1, 0.2, 0.3, 0.4, 0.5]
thresholds = [0, 15000, 30000, 45000, 60000]

taxes = 0
for i in range(len(percents)):
    if i == len(thresholds) - 1 or yearly < thresholds[i+1]:
        taxes += (yearly - thresholds[i]) * percents[i]
        break
    else:
        taxes += (thresholds[i+1] - thresholds[i]) * percents[i]

print("You pay {} taxes".format(taxes))
print("Your taxation percent is: {}%".format(taxes/yearly*100))

输出示例:

# Test 1
Tell your yearly income:100000
You pay 35000.0 taxes
Your taxation percent is: 35.0%

# Test 2
Tell your yearly income:54000
You pay 12600.0 taxes
Your taxation percent is: 23.333333333333332%

非优化方式:

这里的关键是要有一个变量 temp (用 temp = annual 初始化,当您加税时该变量会减小:

Here the key is to have a variable temp (initialized with temp = yearly that decreases when you apply the taxes :

yearly = int(input("Tell your yearly income:"))

one = 0.1
two = 0.2
three = 0.3
four = 0.4
five = 0.5

diff = 15000

temp = yearly
taxes = 0
if yearly in range(1,15000):
    taxes += temp * one
else:
    taxes += diff * one
    temp -= diff
if yearly in range(15001,30000):
    taxes += temp * two
else:
    taxes += diff * two
    temp -= diff
if yearly in range(30001,45000):
    taxes += temp * three
else:
    taxes += diff * three
    temp -= diff
if yearly in range(45001,60000):
    taxes += temp * four
else:
    taxes += diff * four
    temp -= diff
if yearly > 60000:
    taxes += temp * five

print("You pay {} taxes".format(taxes))
print("Your taxation percent is: {}%".format(taxes/yearly*100))

这篇关于Elif和range函数,用于按百分比累进计算征税的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆