Python
Programming Exercises, Gently Explained
87
print('+' + ('-' * (width - ____)) + ____)
# Loop for each row (except the top and bottom):
for i in range(height - 2):
# Print the sides:
print(____ + (____ * (width - 2)) + ____)
# Print the bottom row:
print(___________________________________)
The complete solution for this exercise is given in Appendix A and
https://invpy.com/borderdrawing.py. You can view each step of this program as it
runs under a debugger
at
https://invpy.com/borderdrawing-debug/.
88
E X E R C I S E # 2 9 : P Y R A M I D D R A W I N G
drawPyramid(5)
→
#
###
#####
#######
#########
This exercise continues the generative ASCII art programs of Exercise #27, ―Rectangle
Drawing,‖ and Exercise #28, ―Border Drawing.‖ In
this exercise, your code prints a pyramid of
hashtag characters in any given size.
Exercise Description
Write a drawPyramid() function with a height parameter. The top of the pyramid has one
centered hashtag character, and the subsequent rows have two more hashtags than the previous row.
The number of rows matches the height integer. There are no Python assert statements to check
the correctness of your program. Instead, you can visually inspect the output yourself. For example,
calling drawPyramid(8) would output the following:
#
###
#####
#######
#########
###########
#############
###############
Try to write a solution based on the information in this description. If you still have trouble
solving this exercise, read the
Solution Design and Special Cases and Gotchas sections for
additional hints.
Prerequisite concepts: strings, string concatenation,
string replication, for loops, range()
Dostları ilə paylaş: