Not logged in. Login

Exercise 7

Displaying Dates

Write a Python program current_date.py that prints out the current weekday and time in this format:

Saturday, 04:35 PM

The time module contains a function strftime (“string format time”) that will format the current time. After you import the time module, this statement will print the current time in a different format

print(time.strftime("%b %d, %H:%M"))

Get that statement working first. Then use the online documentation to get the format as required above.

Strings as Objects

Strings in Python are actually objects and contain several methods that can operate on their contents. For example they contain a strip method:

>>> s = "   a b c d       "
>>> s.strip()
'a b c d'

In strings.py, use the replace and upper methods, described in the String Methods Reference to display a string in all uppercase, and with the spaces replaced by underscores:

Enter a string: A string I entered
Uppercase:   A STRING I ENTERED
Underscores: A_string_I_entered 

Working With Images

Do this once at the interactive prompt to install Pillow. Do not include this code in your program.

>>> import pip
>>> pip.main(["install", "pillow"])

In grid.py, write a function draw_grid that takes three arguments: an image size (which will be both the width and height of the image we create), a grid spacing (the number of picels between grid lines), and an output filename. It should create a new Image and draw grid lines. You should draw horizontal grid lines in green, and vertical lines in red, so making this call:

draw_grid(200, 20, "out1.png")

… should produce this image: 1

And this function call:

draw_grid(100, 15, "out2.png")

… should produce this image:

Submitting

Submit all of your work through CourSys.

Updated Fri June 21 2024, 09:22 by ggbaker.