Introduced
We have developed a tax calculator, after entering the salary, printing the tax payable.
A tax calculation formula:
- Taxable income = Salary amount-Various social insurance costs-starting point (3500 yuan)
- Taxable amount = Taxable income x tax Rate-Calculator deduction
Each of the social insurance costs we set in this procedure is 0, do not consider.
The tax rate and the Calculator deduction number corresponding table:
Taxable
Income amount |
Tax rate |
Calculator Deduction number (yuan) |
No more than 1500 yuan |
3% |
0 |
Over $1500 to $4500 |
10% |
105 |
Over $4500 to $9000 |
20% |
555 |
Over $9000 to $35000 |
25% |
1005 |
Over $35000 to $55000 |
30% |
2755 |
Over $55000 to $80000 |
35% |
5505 |
More than 80000 yuan |
45% |
13505 |
For example, if the wage amount is 5000, the taxable income is 1500 (5000-3500) and the taxable amount is 45 yuan (1500 * 3%-0).
The program runs with Python3, and the execution process is as follows
Note that the program executes only one parameter, and the parameter is the wage amount, which needs to be an integer, if the number of arguments is inaccurate or cannot be converted to an integer, you need to print the following line of error message:
Goal
Goals to accomplish:
- Where the program is stored
/home/shiyanlou/calculator.py
- The first parameter that the program executes is the wage amount, which is the way the program executes
./calculator.py 3500
- The number returned by the program retains two decimal places, and if it is an integer, this form still needs to be preserved
xxx.00
.
Cue words
- Turning parameters into integers can be used
int()
, if not correctly converted to integers (for example, int (' ABCD ')), you need to handle the exception and print the above error message.
- The output retains two decimal places that can be formatted using the string's format function, for example,
format(1.2345, ".2f")
a string with two decimal places‘1.23‘
- Command-line arguments are obtained by using the SYS module
sys.argv
, not using input () , which is the sys.argv[0]
script name, sys.argv[1]
as the first parameter, for example:
Finally, because the next challenge will be to use the code that is now written, either 下载代码
save it locally or submit it to your own Github.
Attention
- Try except only need to include code that could have an exception when judging an exception, such as converting an input string to int, without having to include all of the code
- The indentation requirement for each line in Python is strict, so be sure not to Mix tab and space, we recommend using spaces to indent, and indent with 4 spaces.
Knowledge points
- Python3 Program Development
- Variables and data types
- Output
- Command-line arguments
- Operation
- String
- Control structure
- Exception handling
ImportSYSdefcalculator ():Try: A= Int (sys.argv[1]) Salary= a-3500ifSalary > 80000: Cal_salary= Salary * 0.45-13504elifSalary > 55000: Cal_salary= Salary * 0.35-5505elifSalary > 35000: Cal_salary= Salary * 0.3-2755elifSalary > 9000: Cal_salary= Salary * 0.25-1005elifSalary > 4500: Cal_salary= Salary * 0.2-555elifSalary > 1500: Cal_salary= Salary * 0.1-105Else: Cal_salary= Salary * 0.03Print("{:. 2f}". Format (cal_salary))except: Print("Parameter Error") calculator ()
Implementing a tax calculator in Python