CPSC120
Fundamentals of Computer Science

Activity 2

Expressions

Centered Square

Create a Python program that uses turtle graphics to draw a square that is centered in the window and with sides that are 145.7 units in length.

Note that you could write this program without using any expressions. But the purpose of this activity is to practice using Python to perform calculations. So be sure to use an arithmetic expression in your code.

Example

square

Hint

  • To draw the square centered in the window, the turtle should begin the drawing at one of the corners.
  • To get the turtle to a corner, it should first pick up the pen and then move to the corner.
  • To move the turtle to a corner, it will need to move half of the width square horizontally and half of the height of the square vertically. Use an expression to calculate these distances.
  • Don’t forget to put the pen back down before drawing the square!

Age in Seconds

Now that you know expressions, you can use them to compute values that are very difficult to calculate in your head. For example, what if an alien species shows up on Earth and demands to know how old you are? Being aliens, they have no concept of years, only seconds. While computing that by hand might be a chore, Python can rescue us from certain doom.

Details

Create a Python program that prints your age in seconds. Assume there are 365 days in a year. You don't need to compute your exact age, just convert your age in years to seconds. The program should use Python's arithmetic operations to perform the calculations and print the number of seconds nicely labeled.

Example

My age in seconds:

1292976000

Hint

  • You first need to compute how many seconds there are per year.
  • There are 24 hours in a day, 60 minutes in an hour, and 60 seconds in a minute.
  • If you multiply these values together, you get the number of seconds in a year.

Challenge

Computing your age in seconds is pretty easy if you assume that every year has 365 days. But this ignores leap years, how many days it has been since your last birthday, and what time it is now. Modify the program so that it prints out your current age in seconds more precisely.

Background

The turtle module doesn't have the ability to change the screen background color. You can, however, do it yourself by first drawing a filled rectangle as big as the screen and centered in the window.

Details

Create a Python program that uses turtle graphics to draw a filled rectangle with exactly the same width and height as the screen and is perfectly centered on the screen. Use expressions with the turtle.window_width() and turtle.window_height() functions to draw the rectangle. Don’t forget to use turtle.begin_fill() and turtle.end_fill() to fill the rectangle.

Example

background

Hint

  • Because the turtle begins in the center of the screen, you first need to move it to the edge of the screen.
  • If the window is turtle.window_width() wide, then the turtle will need to move turtle.window_width() / 2.0 units to reach the left or right edge of the screen.
  • Likewise, the turtle will need to move turtle.window_height() / 2.0 units to reach the top or bottom edge of the screen.

Challenge

What good is a colored background if there is nothing in the foreground? Draw something on top of the background rectangle.