This preview shows page 1. Sign up to view the full content.
Unformatted text preview: 11/26/2008 Assignment ECS 10
11/24 Due Due Dec. 3. Make some progress on it this week before you go home. Problems Problems installing PIL? Please email me if if you figure out how to fix something, we will put tips on the assignment Web page. No No lecture Wds! Wds! Pastel Program
Define Define a function to fade a color. Use Use the function on every pixel. Write Write a for loop that will do something to every pixel in a square. Double for Loop
1 for x in range(5): 2 for y in range(3): 3 print (x,y) # print the tuple Block under line 1 contains another for loop. For every value of x, line 2 generates every value of y. The for loop in line 2 starts over for every new value of x. (0,0) (1,0) (2,0) (0,1) (1,1) (2,1) … … … Add Add Loop to Program
We We know width and height from the im.size variable. Use getpixel() Use getpixel() and putpixel() methods to read putpixel() and write the color of a pixel. and Call Call the function we wrote to change the color. Repetition Repetition
red = color diff = 255 - red red = red+3*diff/4 green = color diff = 255 - green green = green + 3*diff/4 Replace repetition with a function 1 11/26/2008 Why Replace Repetition
Makes Makes it easier to improve the program – if the code is just in one place, you only have to change it in one place. Fewer Fewer bugs! Neater, Neater, shorter, easier to understand. Functions calling Functions
This This happens all the time. In In a large, complicated program, you have to go from one function to another to follow is going going on. Comments Comments describing what every function does are very important. Two Two lines, sometimes more, at the top of a function. Parameters Parameters and Arguments
def pastel(color): …… outColor = pastel(turkeyColor) Function pastel is called with the variable turkeyColor as argument. The parameter color is a different varaible than turkeyColor, but it gets the same value. Local variables
The The variables inside a function are invisible to the main program. This This is to prevent functions from having unintended side effects in the main program. unintended So So variables inside a function are called local variables. The The parameters of the function are local variables. Example Example
def addTwo(y): y = y+2 return y x = 10 z = addTwo(x) Local variable y is not defined in the main main program. Global Global variables
Variables Variables in the main program can be seen inside a function. So they are called global variables. Functions cannot change global variables, just Functions just see see them. This This again is to prevent unintended side effects. 2 11/26/2008 Global variable version
def addTwo(): y = x+2 return y x = 10 z = addTwo() print 'x,z =', x,z x is a global variable, so it can be seen inside the function. The function can’t change x, so it still needs local variable y. Two Two variables with same name
Python Python will let you write a program with a local variable named x and also a global variable named x.
def addTwo(x): x = x+2 return x x = 10 z = addTwo(x) A perfectly legal Python program, but a very bad idea! Try to avoid this!
It It can be really confusing. You You need to know that it can be done because you will end up doing it by mistake. Wh When you figure it out, fix it by changing the name name of one of the variables. Happens Happens when you re-use variable names, for reexample when you have lots of for loops using i as the variable: for i in range(len(s)): Is it local or global?
If If it is a parameter of the function, it’s local. If If it appears on the left in an assignment in a function, it’s local. If If it appears on the left in an assignment in the main main program, but only on the right in the function, it’s global. If If it appears on the left in both the main both the program and the function, you have two variables with the same name! Danger! 3 ...
View Full Document
- Winter '07