ecs10-11-12

# ecs10-11-12 - Announcements ECS 10 10/31 Assignment...

This preview shows page 1. Sign up to view the full content.

This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 11/13/2008 Announcements ECS 10 10/31 Assignment Assignment due tonight. Many Many Eyes had a bug on their Web page, US counties had disappeared! We sent a bug report, and now it’s back. report and Also, Also, bug on my Web page that was not showing the lecture notes with some version of Explorer is fixed. Midterm Midterm on Monday. Bring a Scantron 2000. Exceptions Exceptions popStr = raw_input(‘Enter the population: ‘) try: pop = float(popStr) # Try conversion except: # Conversion failed! print ‘Not a number.’ pop = None if pop != None: # Put rest of program here. helper.py This This is how helper.py worked. Recall Recall specifically: if helper.isFloat(popStr): pop = float(popStr) else: print ‘Not a number!’ The The expression helper.isFloat(popStr) is a helper.isFloat(popStr) is Boolean, True if conversion will work. Functions Functions Today Today we consider how to put this bit of code into a function that we can include in a module. Any Any program we write with functions could have have been written without them. No new magic…. Makes Makes it easy to pass code around, extend the language. Also Also makes for neater programs. Parts Parts of a Function We We have used lots of functions. Example: x = int (53.66) - takes the float 53.66 and computes the integer 53, value of int(53.66) is int(53.66) 53, variable x gets 53. Function Function "parts of speech": Float Float 53.66 is the argument argument Int Int 53 is the return value return 1 11/13/2008 Defining functions def isFloat(s): Begin Begin defining function with def Parameters go inside parenthesis, eg. (s) eg. Parameters Parameters are variable to hold values of arguments, inside the function. Might Might have no parameters. Might Might have lots of parameters. Colon! Colon! Arguments and Parameters When When I defined isFloat, I had no idea what you isFloat, were going to call the variable containing the input string when you called helper.isFloat. helper.isFloat. But But I needed a variable to put that data (the input input string) into. My My variable - s - is the parameter. The The argument is the variable in your program that holds the input data. Return Return Value return True Functions Functions usually produce an output data value. return is return is a Python command, only used in functions. functions. The The data value after return is the output data return value produced by the function. We We say this value is ‘returned’ by the function. Let’s make a new function Remove Remove commas from a string. We’ll We’ll just put it into helper. def noComma(s): s = s.replace(',' , '') return s It It is OK to change the parameter. It It is OK to return the parameter. From From the outside… Function Function arguments don’t change. After this: reply = raw_input(‘Enter interest rate: ‘) rate = float(reply) reply reply is still a string; the function float() did float() nothing to it. rate rate is a float (if there was no error). Functions Functions we define ourselves are the same; the arguments stay the same, even if the arguments parameters parameters change in the function. Functions Functions in Modules Need Need to import the module before you can use the functions in it. Function isFloat Function isFloat in module helper is called helper helper.isFloat. helper You You can shorten the prefix: import helper as h … if h.isFloat(inStr): … 2 11/13/2008 Functions in Program You You can also define functions in the same file as the program that uses them. Put Put function definitions at the top of the file, right after the imports. Functions must be right Functions defined defined before they can be used! The The code in a function is not run until the function is called, whether it is in a separate module or in the file with the program. On On the inside… What What happens in functions, stays in functions. functions. Changing Changing parameters does not change arguments def add_one(x): dd x = x+1 return x age = 5 next = add_one(age) print "I am",age,"and soon I will be",next 3 ...
View Full Document

{[ snackBarMessage ]}

Ask a homework question - tutors are online