Homework 5, Problem 3:
ASCII Art
[10 extra
points; individual or pair]
Submission: Submit your hw5pr3.py file to the submission server
In
this assignment you will revisit a classic art form: ASCII art!
Important
limitation! For
these problems, do not use Python's string-multiplication or
string-addition operators. Because our
goal is to use loop constructs, use loops to achieve the repetition that those
operators might otherwise provide.
The goal of this problem is to
solidify further your reasoning skills with loops, and nested loops. For many
of the problems (especially the striped diamond), you will have to think
carefully about the value of your loop control variable as your loop or loops
execute. "Debugging by random permutation" -- that is, arbitrarily
changing the values of your loop conditions or variables -- will lead to much
frustration... . The path to success on this assignment is to reason carefully
about your loops.
printSquare
We'll start simple. Write a function printSquare that takes two arguments, a number n
and a character c
and prints a square of size n by n
on the screen.
e.g.
printSquare( 3, '$' )
$ $ $
$ $ $
$ $ $
Note the spaces between the characters. This is natural
if you use Python's print command followed by a
comma.
It is possible to avoid these spaces that Python
insists on. To do so, you will need to use the more complicated function call sys.stdout.write( '$' ), where the '$' can be a variable holding a string as well.
To use this, you will need to have the line import sys at the top of your file. However, this is
entirely optional. Feel free to use print with the spaces!
printRect
Next, write a function named printRect that
takes three arguments, width, height, and symbol, and
prints a width by height rectangle of symbols on
the screen.
printRect( 4, 6, '%' )
% % % %
% % % %
% % % %
% % % %
% % % %
% % % %
Again, in this part, and in all
remaining parts, the space between the characters is optional, as long as the
characters line up correctly.
Answer the following question
in comments in a comment or triple-quoted string in your hw5pr3.py file:
Question:
What does your printRect function do if you pass it negative numbers as input?
Is this a reasonable behavior? If not, modify your code and say what you fixed.
printTriangle
Add a function printTriangle
that takes three
arguments: width
, symbol
,
and rightSideUp
and prints a triangle of symbols on the screen. width
is a number that determines the width of the base of the triangle and rightSideUp
is a boolean that determines whether the triangle is printed right side up (True
)
or upside down (False
).
printTriangle( 3, '@', True )
@
@ @
@ @ @
printTriangle( 3, '@', False )
@ @ @
@ @
@
printBumps
Now, use your printTriangle function to write a
function called printBumps(
num, symbol1, symbol2 )
that will print the specified number
of two-symbol "bumps", where each bump is larger than the last, as in
the following example:
printBumps( 4, '%', '#' )
%
#
%
% %
# #
#
%
% %
% % %
# # #
# #
#
%
% %
% % %
% % % %
# # # #
# # #
# #
#
printDiamond
For these "diamond" functions, you may use string multiplication, but
only for strings of blank spaces, such as ' '*n or the like. Each visible
character should be printed separately, just as in the functions earlier in
this problem. Also, you don't have to
use the string *
operator for strings of spaces, either.
Write a function called printDiamond( width, symbol )
that prints a diamond of symbol
whose maximum width is determined by width
.
printDiamond( 3, '&' )
&
& &
& & &
& &
&
printStripedDiamond
Next, write a function called printStripedDiamond( width, sym1,
sym2)
that prints a "striped diamond" of sym1 and
sym2.
For example:
printStripedDiamond( 7, '.', '%' )
.
. %
. % .
. % . %
. % . % .
. % . % . %
. % . % . % .
% . % . % .
. % . % .
% . % .
. % .
% .
.
printCrazyStripedDiamond
Finally, write a function called printCrazyStripedDiamond( width,
sym1, sym2, sym1Width, sym2Width)
that prints a
"striped diamond" of sym1 and sym2 where the stripes can have varied
widths. sym1Width
determines the width of the stripe made of symbol 1 and sym2Width
determines the width of the stripe made of symbol 2.
For example:
printCrazyStripedDiamond( 7, '.', '%', 2, 1 )
.
. .
. . %
. . % .
. . % . .
. . % . . %
. . % . . % .
. % . . % .
% . . % .
. . % .
. % .
% .
.
If
you have gotten to this point, you have completed problem 3! You should submit your hw5pr3.py
file at the Submission Site.
Next