Week 3
Week 3
Week 3
>>> print_many("hello", 4)
hello
hello
hello
hello
2
Exercise
• Recreate the lines/boxes of stars example from lecture:
*************
*******
***********************************
**********
* *
**********
*****
* *
* *
*****
3
Exercise Solution
stars.py
1 # Draws a box of stars with the given width and height.
2 def box(width, height):
3 print width * "*"
4 for i in range(height - 2):
5 print "*" + (width - 2) * " " + "*"
6 print width * "*"
7
8 # main
9 print 13 * "*"
10 print 7 * "*"
11 print 35 * "*"
12 box(10, 3)
13 box(5, 4)
4
Default Parameter Values
def name(parameter=value, ..., parameter=value):
statements
– Can make parameter(s) optional by specifying a default value
>>> def print_many(word, n=1):
... for i in range(n):
... print word
>>> print_many("shrubbery")
shrubbery
>>> print_many("shrubbery", 4)
shrubbery
shrubbery
shrubbery
shrubbery
5
Parameter Keywords
name(parameter=value, ..., parameter=value)
– Can specify the names of parameters as you call a function
– This allows you to pass the parameters in any order
>>> def print_many(word, n):
... for i in range(n):
... print word
6
Math commands
from math import *
Function name Description Constant Description
ceil(value) rounds up e 2.7182818...
cos(value) cosine, in radians pi 3.1415926...
degrees(value) convert radians to degrees
floor(value) rounds down
log(value, base) logarithm in any base
log10(value) logarithm, base 10
max(value1, value2, ...) largest of two (or more) values
min(value1, value2, ...) smallest of two (or more) values
radians(value) convert degrees to radians
round(value) nearest whole number
sin(value) sine, in radians
sqrt(value) square root
tan(value) tangent
7
Returning Values
def name(parameters):
statements
...
return value
>>> ftoc(98.6)
37.0
8
DrawingPanel
• Instructor-provided drawingpanel.py file must be in the
same folder as your Python program
9
DrawingPanel Example
draw1.py
1 from drawingpanel import *
2
3 panel = DrawingPanel(400, 300)
4 panel.set_background("yellow")
5 panel.canvas.create_rectangle(100, 50, 200, 300)
6 panel.mainloop()
10
Drawing Methods
Java Python
drawLine panel.canvas.create_line(x1, y1, x2, y2)
drawRect, panel.canvas.create_rectangle(x1, y1, x2, y2)
fillRect
drawOval, panel.canvas.create_oval(x1, y1, x2, y2)
fillOval
drawString panel.canvas.create_text(x, y, text="text")
11
Colors and Fill
• Python doesn't have fillRect, fillOval, or setColor.
– Instead, pass outline and fill colors when drawing a shape.
– List of all color names: http://wiki.tcl.tk/16166
– Visual display of all colors
drawcolors.py
1 from drawingpanel import *
2
3 panel = DrawingPanel(400, 300)
4 panel.canvas.create_rectangle(100, 50, 200, 200,
outline="red", fill="yellow")
5 panel.canvas.create_oval(20, 10, 180, 70, fill="blue")
6 panel.mainloop()
12
Polygons
• Draw arbitrary polygons with create_polygon
• Draw line groups by passing more params to create_line
drawpoly.py
1 from drawingpanel import *
2
3 panel = DrawingPanel(200, 200)
4 panel.canvas.create_polygon(100, 50, 150, 0,
150, 100, fill="green")
5 panel.canvas.create_line(10, 120, 20, 160,
30, 120, 40, 175)
6 panel.mainloop()
13
Exercise
• Write a modified Projectile program:
– Draw projectiles traveling at:
• 85 degrees at 30 m/s
• 85 degrees at 60 m/s
• 85 degrees at 120 m/s
14
Animation
• Pause the panel with sleep
animation.py
1 from drawingpanel import *
2
3 panel = DrawingPanel(350, 300)
4 for i in range(20):
5 # clear any previous image
6 panel.canvas.create_rectangle(0, 0, 400, 400,
outline="white", fill="white")
7
8 panel.canvas.create_polygon(20 * i, 50, 20 * i,
100, 20 * i + 50, 75)
9
10 # sleep for 100ms
11 panel.sleep(100)
15