Professional Documents
Culture Documents
☾
CSE 111 |
Programming with Functions
04 Prove Assignment: Writing Functions
Purpose
Prove that you can write functions with parameters and call those
functions
multiple times with arguments.
Problem Statement
Modern computers are capable of performing all sorts of
calculations to produce
numbers. However, they are also capable of
performing calculations to produce
art, illustrations, animations,
movies, and music.
Assignment
During the previous lesson's milestone, you wrote code to draw at
least the sky,
clouds, and ground of an outdoor scene. During this
lesson, you will write code
that draws the remaining objects in your
scene. Your program can draw any
outdoor scene that you like as long
as it meets these requirements:
Helpful Documentation
The prepare content for lesson 2 explains
how to call functions.
https://byui-cse.github.io/cse111-course/lesson04/prove.html 1/4
6/10/22, 7:58 CSE 111 - 04 Prove
Testing Procedure
Verify that your program works correctly by following each step
in this testing
procedure:
1
def draw_pine_trees(canvas):
2
"""Draw three pine trees, each at a fixed location."""
4
# Draw a tree.
5
draw_rectangle(canvas, 300, 70, 320, 120, fill="brown")
6
draw_polygon(canvas, 240, 110, 310, 320, 380, 110,
7
fill="forestGreen")
9
# Draw another tree.
10
draw_rectangle(canvas, 200, 60, 220, 110, fill="brown")
11
draw_polygon(canvas, 140, 100, 210, 310, 280, 100,
12
fill="forestGreen")
13
14
# Draw a third tree.
15
draw_rectangle(canvas, 100, 50, 120, 100, fill="brown")
16
draw_polygon(canvas, 40, 90, 110, 300, 180, 90,
17 fill="forestGreen")
1
def draw_pine_tree(canvas, peak_x, peak_y):
2
"""Draw one pine tree at location (peak_x, peak_y)"""
4
# Compute the coordinates of the skirt and trunk.
5
skirt_left = peak_x - 70
6
skirt_right = peak_x + 70
7
skirt_bottom = peak_y - 210
8
trunk_left = peak_x - 10
9
trunk_right = peak_x + 10
10
trunk_bottom = peak_y - 260
11
12
# Draw the tree trunk.
13
draw_rectangle(canvas, trunk_left, trunk_bottom,
14
trunk_right, skirt_bottom, fill="brown")
15
16
# Draw the tree skirt.
17
draw_polygon(canvas, skirt_left, skirt_bottom, peak_x, peak_y,
18
skirt_right, skirt_bottom, fill="forestGreen")
19
20
21
def main():
22
# Width and height of the scene in pixels
23
scene_width = 800
24
scene_height = 500
25
26
# Call the start_drawing function in the draw2d.py
27
# library which will open a window and create a canvas.
https://byui-cse.github.io/cse111-course/lesson04/prove.html 3/4
6/10/22, 7:58 CSE 111 - 04 Prove
28
canvas = start_drawing("Scene", scene_width, scene_height)
29
30
draw_pine_tree(canvas, 240, 320)
31
draw_pine_tree(canvas, 160, 280)
32
draw_pine_tree(canvas, 100, 250)
33
draw_pine_tree(canvas, 500, 350)
34
draw_pine_tree(canvas, 600, 280)
35
36
# Call the finish_drawing function
37
# in the draw2d.py library.
38 finish_drawing(canvas)
Ponder
After you finish this assignment, congratulate yourself because
you wrote a
Python program with many user-defined functions. As you
wrote your program,
what did you learn about organizing a program
into functions? Did you learn
that you can work more efficiently by
writing a function once and calling it many
times with different
arguments?
Submission
To submit your program, return to I-Learn and do these two
things:
https://byui-cse.github.io/cse111-course/lesson04/prove.html 4/4