< 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

Now that you know expressions, you can use them to compute values that are very difficult to compute 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

In Emacs, create a Python program in a file called age_in_seconds.py in your lab2 directory. The program should print 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 should print the number of seconds nicely formatted an labeled.

Example

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

Hint

You first need to compute how many seconds there are per year. Recall that 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

Of course, those aliens probably use some other time as opposed to just using our seconds. Let's assume for a second that one of their seconds is 5.4 of ours. How would your expression change if this was the case?


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

  • If a single sheet of paper is 0.004 inches thick then when it is folded in half once, it will be twice as thick, 0.004 * 2 = 0.008.
  • If a single sheet of paper is is folded in half twice, it will be twice as thick as when it is folded once, (0.004 * 2) * 2 = 0.016.
  • If a single sheet of paper is folded in half three times, it will be twice as thick as if it were folded twice, ((0.004 * 2) * 2) * 2 = 0.032
  • You can probably see where this is going. Just repeat this until you have found the thickness of folding the paper 10 times.

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.