ecs10-10-15 - Announcements Midterm Mon Oct 22 ECS10 10/15...

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: Announcements Midterm Mon Oct 22 ECS10 10/15 Any notes you want (programs!) Textbook No l l t rs N calculators New TA! Prof. Amenta in lab hours Wds 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 Current program should not crash, on any input. Should exit without computing interest, if given bad input. Program may not do what user wants, but does not crash. Program Structure 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) lR fl ( S i ) 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.") Get and check user input. Set up variables for while loop. While loop. Post-process results of while loop, and print output. Clean Up While writing a program, include lots of print statements When you're done, cut most of them out. PrettyPretty-printing floats In this program, fine to just round off floats to ints while printing: print "Balance =", int(balance) int(balance) It would nice to be able to print two decimal ld i t b bl t i tt d i l places: $35.78 $0.56 1 Lots of ways to do it Python has lots of formatting functions for printing floats. Python has lots of functions for playing around with strings. strings Let's look at some string functions. While loop on a string Basic idea: Convert float to a string strIn Copy strIn to another string strOut, but only keep strOut, two decimal places. places Converting anything to a string is done with str() str() Note on functions >>> x = 4.0/3.0 >>> y = str(x) str(x) >>> x 1.3333333333333333 1 3333333333333333 >>> y '1.33333333333' Copy strIn to strOut Copy one digit at a time. Getting one character out of a string: >>> s = "cow" >>> s[0] 'c' >>> s[1] This is called indexing. 'o' >>> s[2] 'w' >>> The value of x remains unchanged by the assignment statement: y = str(x) ( ) x is the argument of the function str(). Functions do not (should not) change their arguments. Copy using while loop How do we know when we get to the end of a string? Length function len() len() s = "cow" cow y = len(s) # y is now 3 len(s) Argument (input) is a string Value (result, output) is an integer Index should be < len(s) len(s) Copy string using while loop i=0 strOut = "" while i < len(strIn): len(strIn): char = strIn[i] strIn[ strOut = strOut+char print strOut i = i+1 2 Now detect decimal point What to do with it? Lots of possibilities. Make new variable end to hold index at which copying should end. Decide h it's l l D id what i ' real value should b when we h ld be h see the decimal pont. pont. Still crashes! Crashes if not enough decimal places in strIn. strIn. How do we detect this? What do we do about it? 3 ...
View Full Document

{[ snackBarMessage ]}

Ask a homework question - tutors are online