Sawteeth Drawing in Plaintext Mode
Assume that we want to write a program to draw sawteech in text mode as
follows.
Please input the width of the sawteeth -- 5
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
There are plenty ways to design a program for this task.
For example, you may use the statement
print(" " * (N+i), "*")
to print out N+i spaces before the star sign (*).
Then by controlling the value of i with a for-loop, you can generate
output like the sawteech shown above.
An alternative way is to use the string formatting skills you learned in
Chapter 5.
Within a for-loop, you may craft a template string carefully, and use it
to print out a star sign. By specifying the width to be N+i, and
right-justifying the star sign, you can obtain the same result as the
above figure.
Try to run the following code and understand its output.
Then modify it so that it prints out the sawteeth shown above.
for i in range(1, 5):
s = "{0:>" + str(i) + "}"
print(i, s, s.format("*") )