This preview shows page 1. Sign up to view the full content.
Unformatted text preview: Assignments ECS10
10/10 Some Some identical programs for Program 2. Will be talking to students involved, penalties. Program Program 3 is more difficult. Start earlier. Get help (in the lab) early. Write Write your own program. Do not look at a working program while you write yours. Compound interest
Say Say you invest $100 and make 7% annually After After one year you have:
$100 $100 + $100*7/100 = $107 Compound interest
The The more you have the more you make You made $7.00 If If you leave it invested, and make another 7% the next year, you have: $107 + $107*7/100 = $114.48 You made $7.49 Interest Interest on a debt
When When you are paying interest, compound paying interest, interest is a bad thing! Say Say you owe $8000, at an interest rate of 15%, the the size of this debt is going to go up very fast! balance Doubles every seven years. Debt and Payments
Say Say you owe $100 and pay 7% annually After After one year you owe:
$100 $100 + $100*7/100 = $107 ($7 in interest) Then Then you pay $15, so you owe $107-$15 = $107$92 The The next year, you owe: $92 + $92*7/100 = $98.44 ($6.44 in interest) Pay Pay another $15, and get down to $83.44 1 New Assignment
Given Given a debt amount, interest rate and monthly payment, figure out how long it takes to pay off the debt, and how much you end up paying paying in interest. Use Use a while loop – while debt is greater than zero, keep adding interest and subtracting payments. “Compounded “Compounded monthly”
Instead Instead of computing and adding interest every year, do it every month. Use Use interest rate of (7 / 12)% = 0.583% every month month Is Is 9% compounded monthly better than 9% compounded annually? Is it exactly the same? Compute Interest Compounded Monthly
Write Write a program to calculate it. Use Use a while loop to iterate through 12 months See how much total interest is charged. Style Style point: write a few lines, run, write a few more….work in small steps.
1. 1. get some data into some variables 2. 2. compute the interest. 3. 3. allow user to give input Program Crash
Python Python stops and refuses to run your program any more because it contains an error. Nasty Nasty red error messages Your goal as a programmer is for your programs never programs never to crash. Windows Windows crashes sometimes. IDLE crashes sometime. And you say…. Why is this program crashing?
It It tells us the line:
monthlyRate = annualRate/12.0 annualRate/12.0 It It tells us what it doesn’t like: unsupported operand type(s) for /: ''str' and str' 'float‘ Converting strings to numbers
Use Use Python functions: int() int() float() float() Examples: Examples:
x = int(“26”) # x now contains the integer int(“26”) 26 y = float(“7.5”) # y now contains the float 7.5 Function raw_input() Function raw_input() returns a string Cannot Cannot divide a string by 12.0 2 Still crashes!
The The input to float() has to be a string that represents a float. The The input to int() has to be a string that int() represents represents an integer float(“2.366”) # does not crash float(“12”) # does not crash float(“cow”) # crashes! int(“3.45”) int(“3.45”) # crashes! Operators and Functions
Some operators: Some operators:
x = 6-7 6x = 8.0 / 3.44 print ‘Molly’+’,’ if (x < 20) and (x >= 10): Some functions: Some functions:
reply = raw_input(‘Enter weight: ‘) raw_input(‘Enter weight = int(reply) int(reply) 3 ...
View Full Document
This note was uploaded on 03/23/2011 for the course ECS 10 taught by Professor Amenta during the Winter '07 term at UC Davis.
- Winter '07