Write a program that reads in the following information and prints a payroll statement:
Print your money amounts with a dollar sign and two decimal places after the decimal point. Non-money amounts should print with one decimal place after the decimal point. Print a blank line before the input lines and output lines, as shown in the example below.
A sample run is shown below. This run is from the command line; yours can run under IDLE or another IDE. However, the TA should be able to run your program file at the command level. Your output should be exactly like this for these input data. There is a single space after each colon. Include blank lines as indicated.
> python3 Payroll.py Enter employee's name: Smith Enter number of hours worked in a week: 10 Enter hourly pay rate: 9.75 Enter federal tax withholding rate: 0.20 Enter state tax withholding rate: 0.09 Employee Name: Smith Hours Worked: 10.0 Pay Rate: $9.75 Gross Pay: $97.50 Deductions: Federal Withholding (20.0%): $19.50 State Withholding (9.0%): $8.78 Total Deduction: $28.27 Net Pay: $69.22 >Note that the state withholding amount listed in the book differs by 1 cent. This is probably due to a rounding difference in the two versions. (Remember that floating point operations are not exact.) Either answer is fine. Note also that the book version does not display two digits after the decimal point in all cases of monetary amounts; your program should, even if the trailing digit is a zero.
Your file must compile and run before submission. It must also contain a header with the following format:
# File: Payroll.py # Student: # UT EID: # Course Name: CS303E # # Date: # Description of Program:
If you submit multiple times to Canvas, it will rename your file name to something like Payroll-1.py, Payroll-2.py, etc. Don't worry about that; we'll grade the latest version.
But if you do it manually and use a combination of tabs and spaces, it's easy to get it wrong. A tab takes you to the next tab stop on the line, which is set (rather arbitrarily) by your editor. If you combine both tabs and spaces, Python might complain about bad indentation even if it appears that all lines in the block start in the same column. If you're having that problem, try globally substituting all tab characters by some number of spaces (perhaps 8) and see what happens. It will at least show where the indendation issue is.
Format vs. round: A floating point number is stored in memory with a certain precision (usually 32 bits or 64 bits). For example math.pi is 3.141592653589793. Since pi is an irrational, this is still an approximation; there is no finite decimal expansion that represents pi exactly. So when you want to print pi (or any other float) you need to decide how much of the representation you want to retain. If you don't specify, you'll get a decimal approximation to as many significant digits as are stored.
format() is the way you tell Python how you'd like a number printed. It generates a string representation suitable for printing. Notice it doesn't change anything in memory or store a new number. If you want to see pi printed to 4 decimal places you might do:
>>> math.pi 3.141592653589793 >>> format(math.pi, ".4f") '3.1416' >>> math.pi # note formatting didn't change pi 3.141592653589793
round() is a way of generating a new number that you could then store in memory. If you then print it, Python will not display trailing zeros. So round() is not a good way to get a certain precision printed.
>>> round(math.pi, 4) 3.1416 >>> math.pi # you didn't change pi 3.141592653589793 >>> x = 2.5002 >>> format(x, ".2f") '2.50' >>> y = round(x, 2) >>> x 2.5002 >>> y # the rounded value you stored 2.5Bottom line: for printing use format(). Only use round() if you really need a new number that is an approximation of the original, e.g., if you're doing limited-precision arithmetic.
Extra white space in inputs: Try running your program with extra whitespace around the user inputs and see what happens. For numbers, it shouldn't make any difference; when you apply float() or int() it ignores extra white space. For string values (like the name) it will matter; spaces are just more characters that are part of the string input. Later in the semester, we'll learn the method strip() to remove leading and trailing whitespace from an input (or any string).
>>> s = input("name: ") name: Susie Q. >>> s ' Susie Q. ' # note leading and trailing blanks >>> s.strip() # creates a new string, doesn't change s 'Susie Q.' >>> n = input("value: ") value: 12 >>> n ' 12 ' # remember, this is a string >>> int(n) # creates an int, but doesn't change n 12 >>>