Download ECS10 - UC Davis Computer Science

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Python syntax and semantics wikipedia , lookup

APL syntax and symbols wikipedia , lookup

Python (programming language) wikipedia , lookup

Nonblocking minimal spanning switch wikipedia , lookup

String literal wikipedia , lookup

AWK wikipedia , lookup

Standard ML wikipedia , lookup

CAL Actor Language wikipedia , lookup

C syntax wikipedia , lookup

Transcript
Announcements
„
ECS10
„
„
„
10/10
„
„
Interest Compounded
Monthly
„
Costs you more than interest compounded
annually.
Program Crash
„
„
„
„
Why is this program
crashing?
„
„
It tells us the line:
monthlyRate = annualRate
annualRate/12.0
/12.0
It tells us what it doesn’t like:
unsupported operand type(s) for /: ''str
str'' and
'float‘
Function raw_input
raw_input()
() returns a string
„ Cannot divide a string by 12.0
„
First midterm a week from today, in class
Open book, open notes
No computers, no calculators
Bring a Scantron 2000 form, available in
bookstore
We will also ask you to write a short program
Last year’s midterm and program on Web site
Python stops and refuses to run your program
any more because it contains an error.
Nasty red error messages
Y
Your
goall as a programmer is for
f your
programs never to crash.
crash.
Windows crashes sometimes. IDLE crashes
sometime. So annoying! Bad programming!
Converting strings to
numbers
„
Use Python functions:
„ int
int()
()
„ float()
„
Examples:
x = int(“26”)
int(“26”) # x now contains the integer
26
y = float(“7.5”) # y now contains the float
7.5
1
Still crashes!
„
„
„
The input to float() has to be a string that
represents a float.
The input to int()
int() has to be a string that
represents an integer
float(“2.366”) # does not crash
float(“cow”) # crashes!
int(“3.45”)
int
(“3.45”) # crashes!
We’ll solve this with a new function
function..
Operators and Functions
„
x = 66--7
x = 8.0 / 3.44
print
rint ‘Molly’+’,’
‘M ll ’+’ ’
if (x < 20) and (x >= 10):
„
„
„
„
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.
P h doesn’t
Python
d
’ have
h
a function
f
to checks
h k
whether a string can be converted to a float
or to an int
There is a way to do this, but we haven’t
learned the right parts of Python yet….
A helpful module
„
„
„
„
„
„
Helper.py
„
„
„
„
Our module will be called helper
It’s in the file helper.py
You need to have this file in the same folder
as your program so that
h Python
P h can find
f d it.
To get access, at top of program file:
Some functions:
functions:
reply = raw_input
raw_input(‘Enter
(‘Enter weight: ‘)
weight = int(reply)
int(reply)
How to fix?
„
Some operators:
operators:
You need a checking function
We’ll give you a checking function
We put it in a module that you can import
A module is a file that extends Python (a plugplugin, an add
add--on).
Anybody can write a module!
Ability to extend the language makes it really
useful.
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
isFloat(“9.2”) # is True
helper.isFloat (“12”) # True
helper.isFloat(“three”) # False
import helper
2
Looping for input
„
„
The program is not crashing.
It tells the user what is wrong and asks again
for input.
Parts of program
„
„
„
Getting input
Doing computation – while loop
Writing output
Prettier Output
„
You can ask Python to cut off extra decimal
places.
‘% 2f’ % totalInterest
‘%.2f’
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