This preview shows page 1. Sign up to view the full content.
Unformatted text preview: 11/2/2007 Assignment ECS 10
11/02 Due Friday tonight. tonight. FallFall-back positions for partial credit:
Prints out list of points Draws a polygon with the right number of sides D l ih h i h b f id Prof. Ludaescher will lecture Mon and Wds. Wds. Next assignment on the Web on Tues. Question on "helper" module. Another wave Another function example
Test to see if a string is all digits (no decimals, no commas...) Use the in Boolean test.
def isInt1(s): valid = True for char in s: if not (char in "0123456789"): valid = False Drawing two curves, do it with a function. ...now I can change the dot radius in both curves by changing it in one place. It's good not to duplicate information, or code. Is an element in a sequence?
char in "0123456789" Character on left, string on right. right. True if char is a digit. "pig" in ["swine","porker","hog"] String on left, list of strings on right. This expression has the Boolean value False. Calling the function
def isInt1(s): valid = True for char in s: if not (char in "0123456789"): valid = False return valid reply = raw_input("Enter an integer: ") if isInt1(reply): print "That's an integer" 1 11/2/2007 Who sees what
Variables defined in the main program ARE visible in the function. These are called global variables. variables. Usually, they cannot be changed by functions. functions Variables defined in the function (including function arguments) are NOT visible in the main program. These are called local variables. variables. Who sees what
def is_all_digits(s): valid = True for char in s: if not (char in "0123456789"): valid = False return valid reply = raw_input("Enter an integer: ") if is_all_digits(reply): print "That's an integer" reply is defined s is not defined valid is not defined Guideline
Use different variable names in function and main program, EXCEPT when you intend, on purpose, for a function to use a global variable from the main program. Put a comment next to program it so you know that that is what you are doing. Using the same variable names in both places can lead to endless confusion... The "real" isInt
Called like this: reply = raw_input("Enter an integer: ") if reply.isdigit(): print "That's an integer." Special kind of function known as a method. method. If isdigit() was a normal function, it would be called like this: isdigit(reply) Methods
Structure of a method call: String cutting method
Divide string based on some character: sentence = "How do you get the words?" words = sentence.split(" ") Produces a list of littler strings. reply.isdigit() reply.isdigit()
variable; bl first argument function f name Second, third, etc. arguments (if they exist) go inside parenthesis as usual. 2 11/2/2007 Very handy for...
Reading data files This will be our next assignment. 3 ...
View Full Document
This note was uploaded on 04/14/2008 for the course ECS 10 taught by Professor Amenta during the Fall '07 term at UC Davis.
- Fall '07