< Back

Lab 2: Variables and Expressions

As usual, create a directory to hold today's files. All programs that you write today should be stored in this directory.

    $ cd ~/cs120/labs
    $ mkdir lab2
    $ cd lab2
  


Age in Seconds

In Emacs, create a Python program in a file called age_in_seconds.py. In this file, we will compute and output a rough estimate of your age in seconds.

Create the following variables in your program, with the appropriate values for each:

Note these are all constants, so they are written in all upper case. Using these variables, we can compute the value of an additional SECONDS_PER_YEAR variable by multiplying all of the above together.

Create a age_in_years variable, and use it to store your current age in years. Use this variable, as well as the SECONDS_PER_YEAR variable to compute the value for a final variable age_in_seconds.

Don't forget to print the value of age_in_seconds, in a format similar to below!

Example

  $ python3 age_in_seconds.py
  Scotty's age in seconds is 977616000

Boring Origami

A single sheet of paper is 0.004 inches thick. If you fold the paper in half, the two halves of the sheet of paper are together 0.008 inches thick. If you fold the paper in half again the four quarters are together 0.016 inches thick. How thick would the paper be if you folded it in half 10 times?

Details

In Emacs, create a Python program in a file called origami.py. The program should print how thick a piece of paper would be if you folded it in half 10 times. The program should use meaningful names for values and simple statements to make the program more readable. The output should be nicely formatted and labeled.

Example

$ python3 origami.py
A piece of paper folded in half 10 times would be 4.096 inches thick.

Hint

Challenge

How many times do you have to fold a piece of paper in half for its height to reach the moon? Use your program to figure it out. The moon is about 238857 miles from the Earth. The actual distance varies because the moon's orbit is not circular. Change the variable for the number of folds until you find the smallest number of foldes that is greater than the distance to the Moon. Note, you will need to convert the thickness to miles or the distance to the moon to inches to make the comparison correctly.


Turtle ID Card

While you might not be aware of it, Roanoke College switched their MaroonCards to a completely new system. This sounded like a great idea to us, so we figured we could create some ID cards using Turtle for the entire class!

Details

In Emacs, create a Python program in a file called ID_card.py in your lab2 directory. In this program, you are going to use turtle to print the name and intended major of both yourself and your lab partner. Your name should appear on the left side of the screen, and your major should appear directly below it. Your partner's name should appear on the right side of the screen, and their major should appear right below their name.

You must use the turtle.window_height() and turtle.window_width() commands to retrieve the width and height of the Turtle window, and use these commands to compute the y-location of the names and majors.

Make sure you use variables where appropriate. You may also draw any additional patterns as you wish on your card. Just make sure the text is still legible!

Example

Note: The text in the following image has been enlarged to improve readability. Your text does not need to be this large

  $ python3 ID_card.py

Hint

Challenge

This process could even be created for a roster of students, as opposed to ID cards. You probably don't have enough time to talk to everyone in class, but talk to at least two other people, and add their name and intended major to the screen. One to appear on the top edge of the screen, and the other to appear along the bottom edge.