Assignment 3
- Due Jul 13, 2017 by 11:59pm
- Points 3
- Submitting a file upload
Exercise 3-1. Write a function named right_justify that takes a string named s as a parameter and prints the string with enough leading spaces so that the last letter of the string is in column 70 of the display: something like this:
>>> right_justify('monty')
monty
Hint: Use string multiplication.
Python provides a built-in function called len that returns the length of a string. The value of len('monty') is 5.
Exercise 3-2. A function object is a value you can assign to a variable or pass as an argument.
For example, do_twice is a function that takes a function as an argument each time.
def do_twice(f):
f()
f()
An example that uses do_twice() to call a function named print_spam() twice:
def print_spam():
print('spam')
do_twice(print_spam)
Modify do_twice() so that it takes two arguments, a function object and a value, and calls the function twice, passing the value as an argument. Copy the definition of print_twice from the book. Use the modified version of do_twice to call print_twice twice, passing 'spam' as an argument.
Define a new function called do_four that takes a function object and a value and calls the function four times, passing the value as a parameter. There should be only two statements in the body of this function, not four.
Exercise 3-3. Note: This exercise should be done using only the statements and other features we have learned so far.
Write a function that draws a grid like the following:
+ - - - - + - - - - +
| | |
| | |
| | |
| | |
+ - - - - + - - - - +
| | |
| | |
| | |
| | |
+ - - - - + - - - - +
Hints To print more than one value on a line, you can print a comma-separated sequence of values:
print('+', '-')
By default, print advances to the next line, but you can override that behavior and put a space at the end, like this:
print('+', end=' ')
print('-')
The output of these statements is '+ -'.
A print statement with no argument ends the current line and goes to the next line.
Write a function that draws a similar grid with four rows and four columns.
Exercise 3..4 Turtle Graphics
Use the Turtle Graphics to draw an equilateral triangle. Take a screen shot of your triangle (Shift-fan 4) and submit it along with your program.