< Back

Lab 2: Primities 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?


Classmates

It's that time of the class where we ask you to get to know your fellow classmates. Of course, the sole purpose of this activity is not just getting to know one another. It's also a fantastic example of how to use the print statement, and how you can use the results of an expression in a print statement.

Details

In Emacs, create a python program in a file called classmates.py. The program should print a table like the one shown below. However, instead of Sally, Alexander, Graham, and Terry, you should put your and your lab partner's information and information about two classmates sitting near you. The program should not use spaces to align columns. Instead use the tab character, "\t".

The program should also print the average age of the four people. The output should be nicely formatted in a sentence that is all one line. The program should compute the average age using the Python arithmetic operators. When you test your program, be sure to verify that the calculation is correct!

Example

$ python3 classmates.py
Name        Hometown        Intended Major      Age
====        ========        ==============      ===
Sally       Roanoke         Computer Science    18
Alexander   Washington      Math                19
Graham      Charlottesville Physics             19
Terry       Richmond        Undecided           18
The average age is 18.5.

Hint

The functionality of the tab in the terminal is different from a tab in a word processor. Instead of advancing by 4 or 5 places, it advances to the next "tab-stop." You may need to add more "\t" characters than anticipated to format the table nicely.

Challenge

You are only required to have 4 rows in your table. However, it would be very advantageous for you to get to know more people from the class. Time permitting, go talk to as many people in the class as possible, and add them to the table as well.