

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!


First midterm a week from today, in classFirst midterm a week from today, in class Open book, open notesOpen book, open notes No computers, no calculatorsNo computers, no calculators Bring aBring a ScantronScantron 2000 form, available in2000 form, available in bookstorebookstore We will also ask you to write a short programWe will also ask you to write a short program Last year’s midterm and program on Web siteLast year’s midterm and program on Web site
Costs you more than interest compoundedCosts you more than interest compounded annually.annually.
Python stops and refuses to run your programPython stops and refuses to run your program any more because it contains an error.any more because it contains an error. Nasty red error messagesNasty red error messages YYYYour goal as a programmer is for your our goal as a programmer is for yourll ff programsprograms never to crashnever to crash .. Windows crashes sometimes. IDLE crashesWindows crashes sometimes. IDLE crashes sometime. So annoying! Bad programming!sometime. So annoying! Bad programming!
monthlyRatemonthlyRate == annualRateannualRate/12.0/12.
It tells us what it doesn’t like:It tells us what it doesn’t like:
unsupported operand type(s) for /: 'unsupported operand type(s) for /: 'strstr' and' and 'float‘'float‘
Use Python functions:Use Python functions: intint()() float()float()
x =x = intint(“26”)(“26”) # x now contains the integer# x now contains the integer 2626 y = float(“7.5”) # y now contains the floaty = float(“7.5”) # y now contains the float 7.57.
Still crashes!Still crashes!
The input to float() has to be a string thatThe input to float() has to be a string that represents a float.represents a float.
The input toThe input to intint() has to be a string that() has to be a string that represents an integerrepresents an integerrepresents an integerrepresents an integer float(“2.366”)float(“2.366”) # does not crash# does not crash float(“cow”) # crashes!float(“cow”) # crashes! intint(“3.45”) # crashes!(“3.45”) # crashes!
We’ll solve this with a newWe’ll solve this with a new functionfunction..
Operators and FunctionsOperators and Functions
SomeSome operatorsoperators:: x = 6x = 6--7 7 x = 8.0 / 3.44x = 8.0 / 3. print ‘Molly’+’,’print ‘Molly’+’,’rint ‘M ll ’+’ ’rint ‘M ll ’+’ ’ if (x < 20) and (x >= 10):if (x < 20) and (x >= 10): SomeSome functionsfunctions:: reply =reply = raw_inputraw_input(‘Enter weight: ‘)(‘Enter weight: ‘) weight =weight = intint(reply)(reply)
How to fix?How to fix?
We can’t control what the user enters!We can’t control what the user enters!
Need to check user’s input before we doNeed to check user’s input before we do anything with it that might cause a crash.anything with it that might cause a crash.
PPPPython doesn’t have a function to ython doesn’t have a function tohh dd ’’ hh ff checkscheckshh kk whether a string can be converted to a floatwhether a string can be converted to a float or to anor to an intint
There is a way to do this, but we haven’tThere is a way to do this, but we haven’t learned the right parts of Python yet….learned the right parts of Python yet….
You need a checking functionYou need a checking function We’ll give you a checking functionWe’ll give you a checking function We put it in aWe put it in a modulemodule that you can importthat you can import
A helpful moduleA helpful module
AA module is a file that extends Python (a plug- module is a file that extends Python (a plug- in, an addin, an add--on).on). Anybody can write a module!Anybody can write a module! Ability to extend the language makes it reallyAbility to extend the language makes it really useful.useful.
Helper.pyHelper.py
Our module will be calledOur module will be called helperhelper
It’s in the fileIt’s in the file helper.pyhelper.py
You need to have this file in the same folderYou need to have this file in the same folder as your program so that Python can find it.as your program so that Python can find it.hh PP hh ff dd
To get access, at top of program file:To get access, at top of program file:
import helper
Two functions in helperTwo functions in helper
helper.isFloathelper.isFloat(),(), helper.isInthelper.isInt()() Both take a string as inputBoth take a string as input Both return a Boolean value as outputBoth return a Boolean value as output isFloat(“9.2”) # is True helper.isFloat (“12”) # True helper.isFloat(“three”) # False