As usual, create two directories for today's class. Create a
directory called lecture4
under activities, and
a directory called lab4
under labs.
Up until now, we have made some computation, and been done with it. However, most of the time we will want to keep track of the values we compute, for future use and references. Variables allow us to accomplish that.
Create a Python program that prints your age in seconds. The program should use meaningful names for values and simple statements to make the program more readable.
In Emacs, create a Python program in a file called
handshakes.py
. The program should print how many
handshakes it would take for every student at Roanoke to shake hands
with every other student at Roanoke. Assume the number of students at
Roanoke is 1,869. The program should use Python's arithmetic
operations to perform the calculations and should print the number of
handshakes nicely formatted and labeled.
Comments are not the only mechanism we can use in order to make our programs more readable. Choosing appropriate variable names, as well as following some simple guidelines can make it much easier to follow the intent of a program.
All lines should be less than 80 characters (the default width of an Emacs window). The best way to ensure that all lines of code are less than 80 characters is to write simple statements that do only one thing. When a line must span multiple lines:
break after commas.
break before operators.
prefer higher level breaks.
There should be only one variable declaration per line
Initialize variables on the same line as they are declared, if possible.
Declare variables at the beginning of the block of code in which they will be used.
Names for variables should be meaningful. Common abbreviations are OK (avg, max, etc.), but single character names and acronyms should be avoided.
Create a Python program in a file called oil_drum.py
that prints the volume of an oil drum in gallons if the drum's radius
is 11.25 inches and the height is 33.5 inches. The volume of a
cylinder can be computed using the equation, volume = area_of_base
* height, and the area of a circle can be computed using the
equation area = pi * r2. One gallon is
231 cubic inches.
Voyager 1 is a space probe that was launched by NASA in 1977. It is currently the farthest man-made object from Earth.
In Emacs create a python program in a file called
voyager.py
. The program should print how long it takes a
signal from Voyager 1 to reach the Earth and how long it would take
voyager 1 to reach Proxima Centauri at its current velocity. These
calculation should be performed using Python's arithmetic operations
and printed nicely labeled and formatted. Be sure to use variables
where appropriate, and make sure that you follow the coding guidelines
described in class.
Currently, Voyager 1 is approximately 18,206,397,571 km from Earth. The Voyager 1 communicates with radio waves, which travel at the speed of light, 299,792,458 m/s. The Voyager 1's velocity is 61,400 km/h. The nearest star to the Earth is Proxima Centauri at 4.2 light years. Voyager 1's power comes from heat released by the radioactive decay of plutonium. It is projected that Voyager 1 will run out of power in the year 2025.
Make sure to include a comment header which lists who the authors are, as well as the purpose of the file.
When you have finished, create a tar file of your lab4
directory. You can do this by first navigating to your
labs
directory, using the cd
command. Then,
issue the following command.
tar czf tarfilename.tgz lab4/
To submit your activity, go to cseval.roanoke.edu. You should
see an available assignment called Lab Assignment 4
. Only
one of your pair should submit your activity. Make sure both partners
are listed in the header of your file.