Python
The program must include an Id block, and detailed algorithm as a comment block; refer to the sample output for the format. This programming assignment will give you an introduction to the use of data files. The user will be asked for an input and output file name. The input file contains name, ID (4 digit integer), and the annual income for a list of people. In response, your program will verify the ID number and the year's earnings are numeric and if values are verified calculate the amount of income tax which should be paid and print a report similar to the sample below. Tax is calculated according to the table below. If data is Incorrect print an error message and continue processing the next set of data. A sample runs of the program are provided below, with user input shown in bold type. Your program is required to produce output exactly to the form shown below. Notice the way in which special cases should be handled.
Requirements:
Full documentation and good programming practices are required.
Must have at least 4 functions, more is better.
Must use lists to store data (name, income, id, taxes, net)
If your income is less than $3500 Your tax is $0.00
if your income is over but less than Your tax is plus of the amount over
$3,500 $8,000 $0 6% $3,500
$8,000 $20,000 $270.00 11% $8,000
$20,000 $34,000 $1590.00 17% $20,000
$34,000 $54,000 $3970.00 24% $34,000
$54,000 or more $8770.00 32% $54,000
'''
Python version : 2.7
Python program to read a file containing id, name and income of employees and calculate its taxes and net income and write to output file
'''
# function to read the data from file and return the list of data
def readFile(filename):
data = []
try:
with open(filename) as fp:
contents = fp.readlines()
for line in contents:
line_list = line.strip().split()
data.append(line_list) # id, name, income
fp.close()
except IOError:
print("File "+filename+" doesn't exist")
return data
# function to check if the data is valid, is valid calculate the taxes and net income and write the result to output file
def calculate(data, file):
file.write('%-10s%-20s%10s%10s%10s'%('ID','Name','Income($)','Tax($)','Net($)'))
for i in range(len(data)):
if(len(data[i][0])) != 4:
file.write('\n%-10s%-20s%10s%15s' %(data[i][0],data[i][1],data[i][2],'Invalid data'))
else:
try:
id = int(data[i][0])
name = data[i][1]
income = float(data[i][2])
tax = cal_tax(income)
net = income - tax
result = [id,name,income,tax,net]
display(result,file)
except ValueError:
file.write('\n%-10s%-20s%10s%15s' %(data[i][0],data[i][1],data[i][2],'Invalid data'))
# function to calculate and return tax of given income
def cal_tax(income):
if income < 3500:
return 0
elif income < 8000:
return((float(income-3500))*0.06)
elif income < 20000:
return(270 + ((float(income-8000))*0.11))
elif income < 34000:
return(1590 + ((float(income-20000))*0.17))
elif income < 54000:
return(3970 + ((float(income-34000))*0.24))
else:
return(8770 + ((float(income-54000))*0.32))
# function to write data to output file
def display(data, file):
file.write('\n%-10d%-20s%10.2f%10.2f%10.2f'%(data[0],data[1],data[2],data[3],data[4]))
# main function
def main():
# input
in_filename = raw_input('Enter the name of input file : ')
out_filename = raw_input('Enter the name of output file : ')
data = readFile(in_filename)
# if data is present
if(len(data) > 0):
outFile = open(out_filename,'w')
calculate(data,outFile)
outFile.close()
#call the main function
main()
#end of program
Code Screenshot:


Output:
Console:

Input file:

Outfile:

Python The program must include an Id block, and detailed algorithm as a comment block; refer...