7. Exercises¶
Fill out the
main
function below so that you handle two exceptions that may be raised by your call tosome_function
. If this function raises aValueError
, print “value error happening now”; if this function raises aUnicodeError
, print “unicode error happening now”. Make sure your code can handle both errors. (Note: sincesome_function
isn’t filled out, neither exception will be raised when you run the program.)
These next several problems are variations on a theme. Each will have you return a string that consists of a shape built out of #
(hash) characters. It is left up to you to add the code you would need to run your functions (i.e., adding a main
function and calling the respective function). These problems build in difficulty, and are examples in how solving smaller problems can lead you to incrementally build the solutions to larger problems.
Write a function
line(n)
that returns a line with exactlyn
hashes.- Example:
print(line(5))
- Output:
#####
Write a function
square(n)
that returns ann
byn
square of hashes. Utilize yourline
function.- Example:
print(square(5))
Output:
##### ##### ##### ##### #####
Write a function
rectangle(width, height)
that returns a rectangle of the width and height given by the parameters. Again, utilize yourline
function to do this.- Example:
print(rectangle(5, 3))
Output:
##### ##### #####
Write a function
stairs(n)
that prints the pattern shown below, with heightn
. Again, utilize yourline
function to do this.- Example:
stairs(5))
Output:
# ## ### #### #####
Write a function
space_line(spaces, hashes)
that returns a line with exactly the specified number of spaces, followed by the specified number of hashes.- Example:
print(space_line(3,5))
Output:
#This is where the edge is, so there's 3 spaces before hashes #####
Write a function
triangle(n)
that returns an upright triangle of heightn
.- Example:
print(triangle(5))
Output:
# ### ##### ####### #########
Write a function
diamond(n)
that returns a diamond where the triangle formed by the top portion has heightn
. Notice that this means the diamond has2n - 1
rows.- Example:
diamond(5))
Output:
# ### ##### ####### ######### ####### ##### ### #