Exercise Solutions: Functions¶
Part 1: Rectangles¶
Write a function
make_line(size)
that returns a line with exactlysize
hashes.def make_line(size): line = "" for i in range(size): line += "#" return line print(make_line(5))
Console Output
#####
Write a function called
make_square(size)
that returns asize
bysize
string of hashes.def make_square(size): square = "" for i in range(size): square += (make_line(size) + "\n") return square print(make_square(5))
Console Output
##### ##### ##### ##### #####
Write a function
make_rectangle(width, height)
that returns a rectangle with the given width and height. Use yourmake_line
function to do this.def make_rectangle(width, height): rectangle = "" for i in range(height): rectangle += (make_line(width) + "\n") return rectangle print(make_rectangle(5, 3))
Console Output
##### ##### #####
Part 2: Triangles¶
Write a function
make_downward_stairs(height)
that prints the staircase pattern shown below, with the given height.def make_downward_stairs(height): stairs = "" for i in range(height): stairs += (make_line(i+1) + "\n") return stairs print(make_downward_stairs(5))
Console Output
# ## ### #### #####
Write a function
make_space_line(numSpaces, numChars)
that returns a line with exactly the specified number of spaces, followed by the specified number of hashes, followed again bynum_spaces
more spaces.def make_space_line(numSpaces, numChars): space_line = "" for i in range(numSpaces): space_line += " " for i in range(numChars): space_line += "#" for i in range(numSpaces): space_line += " " return space_line print(make_space_line(3, 5))
Console Output
___#####___
Write a function
make_isosceles_triangle(height)
that returns a triangle of the given height.def make_isosceles_triangle(height): triangle = "" for i in range(height): triangle += (make_space_line(height - i - 1, 2 * i + 1) + "\n") return triangle print(make_isosceles_triangle(5))
Console Output
# ### ##### ####### #########
Part 3: Diamonds¶
Write a function
make_diamond(height)
that returns a diamond where the triangle formed by the top portion has the given height.def make_diamond(height): diamond = "" triangle = make_isosceles_triangle(height) diamond += triangle[:-1] for i in range(len(triangle)-1, -1, -1): diamond += triangle[i] return diamond print(make_diamond(5))
Console Output
# ### ##### ####### ######### ######### ####### ##### ### #
Optional Mission¶
Refactor your functions so that they take a single character as a parameter,
and draw the shapes with that character instead of always using '#'
.
def make_line(size, char):
line = ""
for i in range(size):
line += char
return line
print(make_line(8, '&'))
Console Output
&&&&&&&&
Make the new parameter optional, with default value '#'
.
def make_line(size, char = "#"):
line = ""
for i in range(size):
line += char
return line
print(make_line(8))
Console Output
########