Unformatted text preview: Announcements ECS10
10/10 First First midterm a week from today, in class Open Open book, open notes No No computers, no calculators Bring Bring a Scantron 2000 form, available in bookstore We We will also ask you to write a short program Last Last year’s midterm and program on Web site Interest Compounded Monthly
Costs Costs you more than interest compounded annually. 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. So annoying! Bad programming! 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 1 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(“cow”) # crashes! int(“3.45”) int(“3.45”) # crashes! We’ll We’ll solve this with a new function. function. 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) How to fix?
We We can’t control what the user enters! Need Need to check user’s input before we do anything with it that might cause a crash. Python doesn’t have a function to checks whether whether a string can be converted to a float or to an int There There is a way to do this, but we haven’t learned the right parts of Python yet…. A helpful module
You You need a checking function We’ll We’ll give you a checking function We We put it in a module that you can import module A module is a file that extends Python (a plugplugin, an add-on). addAnybody Anybody can write a module! Ability Ability to extend the language makes it really useful. Helper.py Helper.py
Our Our module will be called helper helper It’s It’s in the file helper.py helper.py You You need to have this file in the same folder as your program so that Python can find it. To To get access, at top of program file: import helper Two functions in helper
helper.isFloat(), helper.isInt() helper.isFloat(), helper.isInt() Both Both take a string as input Both Both return a Boolean value as output isFloat(“9.2”) # is True helper.isFloat (“12”) # True helper.isFloat(“three”) # False 2 Looping for input
The The program is not crashing. It It tells the user what is wrong and asks again for input. Parts of program
Getting Getting input Doing Doing computation – while loop Writing Writing output Prettier Output
You You can ask Python to cut off extra decimal places. ‘%.2f’ % totalInterest lI First part says output will be a string with two digits after decimal place, made out of a float % operator connects format and floating point data 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