簡體   English   中英

在Python中使用if else語句的強力球獎金

[英]Powerball winnings using if else statements in python

我正在嘗試解決有關彩票中獎的問題。 眾所周知,一個人中了彩票后,他們並不總是會拿走全額。 它們在不同的括號內被征稅,我正在嘗試創建一個Python程序,該程序告訴我們對winnngs征稅的金額。

在評估任何稅款之前,優勝者將獲得6300美元的標准扣除額和4000美元的個人免稅額。 因此,在我們找出要征稅的金額之前,我們先按以下公式找到應納稅額

應納稅所得額=所得額-(標准扣除額+個人免稅額)

此后,中獎金額被歸類在這些金額括號中。

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

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

37,450至90,750美元---- 25%

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

189,300至411,500美元---- 33%

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

$ 413,200 + ---- 39.6%

例如。 如果某人贏得$ 54000的應納稅所得額= $ 54000- $ 6300- $ 4000 = $ 43,700,將被征稅。 其中:

$ 9225的稅率為10%= $ 922.50,尚有$ 34,475待征稅

$ 28,225稅率為15%= $ 4233.75,還有$ 6,250未課稅

6,250美元的稅率為25%= 1,562.50美元

欠款總額= 922.5 + 4233.75 + 1562.5 = 6718.75美元(或舍入為6,719美元)

這是我的代碼。

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 = restder2 * 0.25 NameError:名稱“ remainder2”未定義

如果應納稅所得額大於37450,則總是會發生此錯誤,因為只有當應納稅所得額在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)

我認為該解決方案比您的解決方案要健壯一些,但是它確實確實包含了代碼的精神。

原始問題的答案是,該符號未定義,因為該定義出現在未采用的“ if”語句內,例如,在此塊中:

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

如果您的獎金少於9225,則代碼remainder2 = taxablewinnings - 37450不會執行。 並且,如果您的獎金大於37450,則該區塊將永遠不會執行,因為它們太高了。 您在這里有邏輯錯誤。 (以及整個過程。)

另外,如果要征稅的金額不是全額,您該如何處理?

例如,如果我的應納稅所得額為40000,那肯定有資格成為taxablewinnings > 9225應納稅所得額。 但是40000不匹配應taxablewinnings <= 37450 因此,您將我所有的獎金都推到了較高的稅級。

否則,您會發現,因為您完全跳過了上面的代碼塊,因此尋找的是從未初始化的remainder2

相反,您希望從收益中抽出一點錢,即使這些收益高於稅額上限。 因此,如果40000是應納稅的獎金,則將其以0.15的稅額征稅,然后繼續。

taxes_due = 0.0

if taxablewinnings < 0:
    taxablewinnings = 0

if taxablewinnings > 0:
    rate = 0.10
    lower_limit = 0
    upper_limit = 9225
    if taxablewinnings <= upper_limit:
        taxes_due += rate * (taxablewinnings - lower_limit)
    else:
        taxes_due += rate * (upper_limit - lower_limit)

if taxablewinnings > 9225:
    rate = 0.15
    lower_limit = 9225
    upper_limit = 37450
    if taxablewinnings <= upper_limit
        taxes_due += rate * (taxablewinnings - lower_limit)
    else:
        taxes_due += rate * (upper_limit - lower_limit)

您可以(我希望)從這里看到模式。 顯然,最后一個括號沒有上限,因此會更簡單一些。 祝好運。

根據您的解釋,因為我來自美國以外,那么這可能就是您想要的

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

測試

>>> calculate_tax(54000)
6718.75
>>> 

與其在您的代碼中一次 使用,不如使其成為一個函數,以便可以多次使用,現在關於代碼,第一部分是自我解釋,現在是有趣的部分for循環:在這里我們進行迭代在我們有一些要征稅的地方或直到我們用完括號之前,在tax_brackets之上,正如您所解釋的,我們取的金額等於括號邊界之間的差額,但是如果該金額超出應納稅的金額,則取剩余的金額,然后應用當前括號的稅並減去已用金額。

編輯

上一個函數可以用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
>>> 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM