CSC 15 Lab 4: While Loops Due next Wednesday 0. Write a program to calculate and keep track of the sales tax paid on a number of items. That is, you want to purchase a number of items and for each item, you want calculate the sales tax, as well as keep track of the total price of all items with and without tax. The output of the program should look something like the following: ------------- enter item price in dollars, 0 to quit: 3.99 tax amount on this item = 0.33915 total payment amount = 4.32915 enter item price in dollars, 0 to quit: 12.95 tax amount on this item = 1.10075 total payment amount = 18.3799 enter item price in dollars, 0 to quit: 19.99 tax amount on this item = 1.69915 total payment amount = 40.06905 enter item price in dollars, 0 to quit: 24.95 tax amount on this item = 2.12075 total payment amount = 67.1398 enter item price in dollars, 0 to quit: 0 tax amount on this item = 0.0 total payment amount = 67.1398 total price without tax = 61.88 total sales tax paid = 5.2598 -------------- Note that the individual tax amount and the total price with tax is printed for each iteration of the loop and the total price without tax and the total sales tax amount is printed after the loop terminates (when user enters zero for price). You need to keep track of all of these values. To write this program, first determine how many variables you need, and what each variable will be used for. ### USE COMMENTS TO CLARIFY! ### for example: rate = 0.085 # 8.5 percent sales tax rate (1.0 = 100 percent) Also work out the math you need to do on paper before coding. *************************************************************** 1. Implement the number guessing game where the computer does the guessing, as described in class. Decide on the variables you'll use, and sketch the algorithm on paper before proceeding. COMMENT CLEARLY. Output should be similar to: --------- think of a number between 0 and 1024 My guess is 512 Is the number (e)qual, (l)ess or (g)reater? g My guess is 768 Is the number (e)qual, (l)ess or (g)reater? l My guess is 640 Is the number (e)qual, (l)ess or (g)reater? l My guess is 576 Is the number (e)qual, (l)ess or (g)reater? g My guess is 608 Is the number (e)qual, (l)ess or (g)reater? l My guess is 592 Is the number (e)qual, (l)ess or (g)reater? l My guess is 584 Is the number (e)qual, (l)ess or (g)reater? g My guess is 588 Is the number (e)qual, (l)ess or (g)reater? g My guess is 590 Is the number (e)qual, (l)ess or (g)reater? l My guess is 589 Is the number (e)qual, (l)ess or (g)reater? g You cheated! You rotten earthling! --------- Note that your while loop stops when one of two conditions are met: 1. when the human enters e indicating that the right number has been guessed 2. when the computer decides that the human is cheating How do you write such a loop? You can't just write while answer != "e": ... since that'll only account for the first condition. You can do the following: use a seperate boolean variable: cheated = False # will be set to true if cheating detected. while answer != "e" and cheated == false: ... Now to stop the loop, you just set cheated to True. There are other ways to stop the loop. HOWEVER, YOU MAY NOT USE ARCHAIC COMMANDS such as 'break' or 'continue'. You must terminate a loop using an appropriate boolean expression Note: to take user input for this program, it is advised to use: response = raw_input("enter e for equal, l for less than g for ...") raw_input reads everything as strings, and there is no need for the user to type the "" marks. ************************************************************************** 2. This problem asks you to calculate an amortization schedule, which is used by banks to keep track of loans. When you make a payment on a car or house, part of the payment is for the principal and part of it is for the interest, and the interest part is sometimes tax deductible. Each iteration of your loop should print the amount of principal paid for that month, the amount of interest paid and the remaining principal balance. Your program should also print the monthly payment amount (M below) before the loop starts. The following description of the algorithm involved is taken from the website www.hughchou.org/calc/formula.htm --------- First you must define some variables to make it easier to set up: # P = principal, the initial amount of the loan # I = the annual interest rate (from 1 to 100 percent) # L = length, the length (in years) of the loan, or at least the length over which the loan is amortized. The following assumes a typical conventional loan where the interest is compounded monthly. First I will define two more variables to make the calculations easier: # J = monthly interest in decimal form = I / (12 x 100) # N = number of months over which loan is amortized = L x 12 Okay now for the big monthly payment (M) formula, it is: J M = P x ------------------------ 1 - ( 1 + J ) ^ -N where 1 is the number one (it does not appear too clearly on some browsers) So to calculate it, you would first calculate 1 + J then take that to the -N (minus N) power, subtract that from the number 1. Now take the inverse of that (if you have a 1/X button on your calculator push that). Then multiply the result times J and then times P. Sorry, for the long way of explaining it, but I just wanted to be clear for everybody. The one-liner for a program would be (adjust for your favorite language): M = P * ( J / (1 - (1 + J) ** -N)) So now you should be able to calculate the monthly payment, M. To calculate the amortization table you need to do some iteration (i.e. a simple loop). I will tell you the simple steps : Step 1: Calculate H = P x J, this is your current monthly interest Step 2: Calculate C = M - H, this is your monthly payment minus your monthly interest, so it is the amount of principal you pay for that month Step 3: Calculate Q = P - C, this is the new balance of your principal of your loan. Step 4: Set P equal to Q and go back to Step 1: You thusly loop around until the value Q (and hence P) goes to zero. ---------------- This description is very detailed, but the formulas it gives are a bit more complicated than necessary. In particular, it uses a lot of variables. Can you figure out a way to write the same algorithm using less variables? You'll still need variables to represent the basic parameters of principal balance, interest rate and length, but what about the other ones like Q? Another thing to be aware of: when testing if two floating point numbers are equal, we often want to consider, for example, 1.5 and 1.499999 to be equal. So using the == symbol is not always appropriate. A better way is to test if the absolute value of their difference falls below some threshold: abs(a-b)<0.000001 ************************************** 4. Modify part 3 above so that it is encapsulated inside a function: def amortization(P,I,L): ... Instead of taking user input or using fixed values for the principal, interest rate, and length, you will instead define a function parameterized by these values. Call your function several times with different values.