我將使用基本運算符制作一個 python 程序來請求用戶的收入,以便計算 2017 年和 2018 年的累進稅。輸出應如下所示:Income: 150002017 tax: (tax amount here) #will skip the math for samples2018 tax: (tax amount here)我的程序現在為 2017 / 2018 生成兩個打印,但將停止在 2018 嵌套 if 括號 82501 到 157500(嵌套在第 4 個 elif 中)......這是我的程序截至目前,因為它很長我會標出它停止工作的地方。income = float(input("Enter your income to calculate tax: "))#Loop input/calculationswhile income > 0: print("----------------------------------------------") if income >= 0 and income <= 9325: bracket1 = income * 0.10 tax2017 = bracket1 print("Income: ",income) print("2017 tax: ",format(tax2017,'.2f')) if income >= 0 and income <= 9525: newbracket1 = income * 0.10 tax2018 = newbracket1 print("2018 tax: ",format(tax2018,'.2f')) income = float(input("\nEnter your income as and integer with no commas: ")) elif income >= 9326 and income <= 37950: bracket1 = 9325 * 0.10 bracket2 = (income - 9326) * 0.15 tax2017 = bracket1 + bracket2 print("Income: ",income) print("2017 tax: ",format(tax2017,'.2f')) if income >= 9526 and income <=38700: newbracket1 = 9526 * 0.10 newbracket2 = (income - 9525) * 0.12 tax2018 = newbracket1 + newbracket2 print("2018 tax: ",format(tax2018,'.2f')) income = float(input("\nEnter your income as and integer with no commas: ")) elif income >= 37951 and income <= 91900: bracket1 = 9325 * 0.10 bracket2 = (37950 - 9325) * 0.15 bracket3 = (income - 37951) * 0.25 tax2017 = bracket1 + bracket2 + bracket3 print("Income: ",income)我已經標記了行不通的行。只是為了澄清,嵌套的 if 和打印之前的輸出 2017 年和 2018 年的結果,但當收入在標記范圍內或更大時,只會打印 2017 年的稅。
1 回答

慕婉清6462132
TA貢獻1804條經驗 獲得超2個贊
只需解決程序將通過的語句
income = 82502.0
將進入
elif income >= 37951 and income <= 91900:
但是對于2018年的計算,它不會滿足里面的條件
if income >= 38701 and income <= 82500:
所以它沒有辦法計算2018。它不會到達你標記它的地步。
2017 年的計算應獨立于 2018 年的計算。在很多情況下,您的計算不會打印 2018 年的稅款。
但是您確實進行了很好的測試,可以看到您的程序無法正常工作。邊緣情況通常會帶來問題。
添加回答
舉報
0/150
提交
取消