ecs10-10-12 - Compound interest ECS10 10/12 Say you invest...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Compound interest ECS10 10/12 Say you invest $100 and make 7% annually After one year you have: $100 + $100*7/100 = $107 You made $7.00 If you leave it invested, and make another 7% the next year, you have: $107 + $107*7/100 = $114.48 You made $7.49 Compound interest The more you have the more you make "Compounded monthly" Instead of computing and adding interest every year, do it every month. Use interest rate of (7/12)% = 0.583% every month Is 9% compounded monthly better than 9% compounded annually? Is it exactly the same? balance Doubles every seven years. Compute Interest Compounded Monthly Write a program to calculate it. Use a while loop to iterate through 12 months See how much you make on $100 Style point: write a few lines, run, write a few in small steps. 1. get some data into some variables 2. compute the interest. 3. allow user to give input Program Crash Python refuses to run your program because it contains an error. Nasty red error messages Your goal as a programmer i f your programs Y l is for never to crash. crash. Windows crashes sometimes. IDLE crashes sometime. And you say.... 1 Why is this program crashing? It tells us the line: monthlyRate = annualRate/12.0 annualRate/12.0 It tells us what it doesn't like: unsupported operand type(s) for /: ''str' and str' 'float` Converting strings to numbers Use Python functions: int() int() float() Examples: x = int("26") # x now contains the integer 26 int("26") y = float("7.5") # y now contains the float 7.5 Function raw_input() returns a string raw_input() Cannot divide a string by 12.0 Still crashes! The input to float() has to be a string that represents a float. The input to int() has to be a string that int() represents an integer float("2.366") # does not crash float("12") # does not crash float("cow") # crashes! int("3.45") int("3.45") # crashes! How to fix? We can't control what the user enters! Need to check user's input before we do anything with it that might cause a crash. There i Th is not a built i f b il in function i P h that i in Python h checks whether a string can be converted to a float or an int There is a way to do this, but we haven't learned the right parts of Python yet.... A helpful module You need a checking function We'll give you a checking function We write a module that you can import Last time: import random .... coin = random.randint(1,2) random.randint(1,2) Anybody can write a module Writing a module can add new functions and other language features. Ours will be called helper It's i h file I ' in the fil h l You need to have this file in the same folder as your program so that Python can find it. Modules that come with Python (like random) random) are installed in other folders that Python checks automatically. 2 Two functions in helper helper.isFloat(), helper.isInt() helper.isFloat(), helper.isInt() Both take a string as input Both return a Boolean value as output goodInput = isFloat("9.2") isFloat("9.2") # now goodInput == True goodInput = isFloat("12") isFloat("12") # goodInput == True goodInput = isFloat("three") isFloat("three") # now goodInput == False Exit with error message The program is not crashing. It tells the user what is wrong and exits normally. It i h I might not d exactly what the user wants, b do l h h but it is not broken. It does what it knows how to do correctly. Clean Up While writing a program, include lots of print statements When you're done, cut them out. The user doesn t doesn't want all that information, just the information answer. Blocks of Program import helper principal = 100.00 rateString = raw_input("Enter annual interest rate:") goodInput = helper.isFloat(rateString) if not goodInput: print "Not a valid interest rate." else: annualRate = float(rateString) monthlyRate = annualRate/12.0 balance = principal month = 0 while month < 12: balance = balance+monthlyRate/100.0*balance month = month+1 eir = balance-principal print "interest earned is",eir raw_input("Press enter to exit.") Interest on a debt When you are paying interest, compound interest is a bad thing! Say you owe $8000, at an interest rate of 15%, and you pay it off by paying $200 a month... month 3 ...
View Full Document

Ask a homework question - tutors are online