python中使用if-else语句的Powerball赢取

2024-04-29 04:13:13 发布

您现在位置:Python中文网/ 问答频道 /正文

我想解决一个有关彩票中奖的问题。我们都知道,当一个人中了彩票后,他们并不总是全额领取。他们在不同的括号里交税,我正试图创建一个python程序,告诉我们我们对赢来的钱征税的金额。在

在评估任何税款之前,获胜者将获得6300美元的标准扣除额和4000美元的个人免税额。所以在我们计算出应纳税额之前,我们先通过公式计算出应纳税额

应税奖金=奖金-(标准扣除+个人免税)

在这之后,中奖金额被分类在这些金额范围内。在

$0 to $9,225----10%

$9,225 to $37,450----15%

$37,450 to $90,750----25%

$90,750 to $189,300----28%

$189,300 to $411,500----33%

$411,500 to $413,200----35%

$413,200+ ----39.6%

例如。如果一个人赢了54000美元 应税奖金=$54000-$6300-$4000 =43700美元是将要征税的金额。其中:

9225美元的税率为10%=922.50美元 剩下34475美元尚未缴税

28225美元的税率为15%=4233.75美元 剩下6250美元还没交税

6250美元的税率为25%=1562.50美元

欠款总额=922.5+4233.75+1562.5=6718.75美元(或6719美元四舍五入)

这是我的密码。在

winnings = float(input("Please enter your Winning amount"))                

tax = 0                                                                    
standardDeduction = 6300                                                   
personalExemption = 4000                                                   


taxablewinnings = winnings - (standardDeduction+personalExemption)  



if taxablewinnings > 0 and taxablewinnings <= 9225:                    
    rate1 = 9225*0.10                                                  
    remainder1 = taxablewinnings-9225                                  

if taxablewinnings > 9225 and taxablewinnings <= 37450:                
    rate2 = remainder1*0.15                                            
    remainder2 = taxablewinnings-37450                                 

if taxablewinnings > 37450 and taxablewinnings <= 90750:               
    rate3 = remainder2*0.25                                            
    remainder3 = taxablewinnings-90750                                 

if taxablewinnings > 90750 and taxablewinnings <= 189300:              
    rate4 = remainder3*0.28                                            
    remainder4 = taxablewinnings-189300                                

if taxablewinnings > 189300 and taxablewinnings <= 411500:             
    rate5 = remainder4*0.33                                            
    remainder5 = taxablewinnings-411500                                

if taxablewinnings > 411500 and taxablewinnings <= 413200:             
    rate6 = remainder5*0.33                                            
    remainder6 = taxablewinnings-413200                                

if taxablewinnings > 413200:                                           
    rate7 = remainder6*0.396                                           

else:                                                                  
    print("Invalid winnings input")                                    

if(winnings > 0):                                                          
    print("Your tax is: $%f" % tax)                                        

我得到了错误

rate3 = remainder2*0.25 NameError: name 'remainder2' is not defined


Tags: andtoinput标准if金额tax奖金
3条回答

如果taxablewinnings大于37450,则始终会发生此错误,因为只有在taxablewinnings在37450到92250范围内时才定义remainder2。在

我重写了你程序的某些部分:

winnings = int(raw_input("Amount won: "))

STD_DEDUCTION = 6300
PERSONAL_EXEMPTION = 4000
TAX_BRACKETS = [(0, 0), (9225, .1), (37450, .15), (90750, .25),
                (189300, .28), (411500, .33), (413200, .35)]

taxable = winnings - (STD_DEDUCTION + PERSONAL_EXEMPTION)
tax = 0

for i in xrange(1, len(TAX_BRACKETS)):
    value = TAX_BRACKETS[i][0] - TAX_BRACKETS[i-1][0]
    percent = TAX_BRACKETS[i][1]
    amt_to_tax = taxable if taxable < value else value
    tax += amt_to_tax * percent
    taxable -= amt_to_tax

tax += taxable * .396

print "Winnings: {}\nTax: {}\nWinnings after taxes: {}".format(
    winnings, tax, winnings - tax)

我认为这个解决方案比您的解决方案更健壮一些,但它仍然真正包含了代码的精神。在

按照你的解释,因为我来自美国以外的地方,那么这可能是你想要的

standardDeduction = 6300                                                   
personalExemption = 4000
tax_brackets = [ (     0,   9225, 0.10),
                 (  9225,  37450, 0.15),
                 ( 37450,  90750, 0.25),
                 ( 90750, 189300, 0.28),
                 (189300, 411500, 0.33),
                 (411500, 413200, 0.35),
                 (413200,   None, 0.396) ]

def calculate_tax(total):
    no_tax = standardDeduction + personalExemption
    taxable = total - no_tax
    total_tax = 0.0
    for min_val, max_val, tax in tax_brackets :
        if taxable <= 0:
            break        
        amount = (max_val - min_val) if max_val is not None else min_val
        if taxable <= amount:
            amount = taxable
        total_tax += amount * tax
        taxable -= amount
    return total_tax

试验

^{pr2}$

与其像在代码中那样只使用来做,不如让它成为一个函数,这样它可以被多次使用,现在关于代码,第一部分是不言自明的,现在有趣的部分是for循环:在这里,我们迭代tax_括号,而我们有东西要征税,或者直到括号用完为止,如你解释说我们取一个等于两个档次之间的差额的金额,但是如果这个金额超过了应纳税的金额,我们就用剩下的部分来代替,然后应用当前等级的税款,然后减去使用的金额。在

编辑

前面的函数可以用if表示,如下所示

standardDeduction = 6300                                                   
personalExemption = 4000
no_tax = standardDeduction + personalExemption

total =  float(input("Please enter your Winning amount: ")) 

taxable = total - no_tax
total_tax = 0.0

if taxable > 0: # brackets 1
    amount = 9225 # - 0
    if taxable <= amount:
        amount = taxable
    total_tax += amount * 0.1
    taxable -= amount

if taxable > 0: # brackets 2
    amount = 37450 - 9225
    if taxable <= amount:
        amount = taxable
    total_tax += amount * 0.15
    taxable -= amount

if taxable > 0: # brackets 3
    amount = 90750 - 37450
    if taxable <= amount:
        amount = taxable
    total_tax += amount * 0.25
    taxable -= amount

if taxable > 0: # brackets 4
    amount = 189300 - 90750
    if taxable <= amount:
        amount = taxable
    total_tax += amount * 0.28
    taxable -= amount 

if taxable > 0: # brackets 5
    amount = 411500 - 189300
    if taxable <= amount:
        amount = taxable
    total_tax += amount * 0.33
    taxable -= amount

if taxable > 0: # brackets 6
    amount = 413200 - 411500 
    if taxable <= amount:
        amount = taxable
    total_tax += amount * 0.35
    taxable -= amount

if taxable > 0: # brackets 7
    amount = 413200 
    if taxable <= amount:
        amount = taxable
    total_tax += amount * 0.396
    taxable -= amount

if total > 0:
    print("you win",total,"and you have to paid",total_tax,"in tax")
else:
    print("Invalid winnings input") 

(这几乎是函数的逐级直译)

试验

Please enter your Winning amount: 54000
you win 54000.0 and you have to paid 6718.75 in tax
>>> 

相关问题 更多 >