This portion of the test is worth 40 total points. You may reference any files within your directory, as well as the Internet. As usual, you are not allowed to directly copy any code from the Internet without citation. You should also follow all coding standards discussed thus far.
Create a directory called test1
under cs120.
Make sure all files you work on today are stored in this directory.
cd cs120 mkdir test1 cd test1
Create a file called question_6.py
. Put your answers to
the following questions there.
(5 points each, 25 points total)
Create two variables named current_year
and birth_year
. Store the current year and your birth
year in the proper variables.
Using these variables, write an expression to print your current age.
Write a function age_in_leap_years
, which takes a
single parameter representing the age of the requester, and prints
an estimate of how old they would be assuming they were born on
Feb. 29th. Assume that a leap year has occurred
every 4 years since their birth.
Use this function to print how old you would be if you were born on Feb. 29th.
Write a function compute_circle_area
. This
function should take one parameter, the radius of a circle. It
should return the area of a circle with the specified radius.
Recall the area of a circle can be computed using the following equation: area = π × radius2
Using this function, print the area of a circle that has a radius equivalent to your (actual, not leap year) age you computed above.
Using the circle
method from the turtle
module, draw a circle with the radius you used above centered
around (0, 0).
Write a function called circumscribe_circle
, which
takes three parameters: the center x coordinate, the center y
coordinate, and the radius of the inscribed circle. This
function should use a for loop to draw a square which
circumscribes the circle that is specified in the formal
parameters of the function.
Using this function to draw a square around the circle drawn above.
My pancreas attracts every other
"Weird Al" Yankovic in Pancreas
Pancreas in the universe
With a force proportional
To the product of their masses
And inversely proportional
To the distance between them...
Who would have thought that real physics would appear in such an absurd song? It's true, your pancreas does attract every other pancreas in the universe. The force is just incredibly weak. We might as well find out what that force is, anyways.
The gravitational force between two objects can be computed using the equation:
F = G × M1 × M2 / r2
Where the gravitational constant G = 6.673 × 10-11, M1 and M2 are the masses (in kg) of the objects in question, and r is the distance (in meters) between the centers of mass of the two objects.
Create a file called question_7.py
, write a function to
compute this gravitational force. Use this function to determine
the gravitational attraction of your pancreas to your neighbors.
Assume the average pancreas weighs 80 grams, and that your pancreas
is about 1 meter away from your neighbor.
Of course, that information isn't very useful. However, the gravitational force between astronomical objects might be. The most massive object in our solar system is the Sun. With a mass of 1.981 × 1030 kg, its mass is over 300,000 times that of the Earth (5.97 × 1024 kg). Compute the gravitational force between the Sun and the Earth, assuming the average distance between them is 147 million km.
The Earth's orbit is not perfectly circular. The elliptical nature of the orbit causes the distance between the Earth and the Sun to vary by about 5 million km over the course of the year (about 2.5 million km in either direction). Using a for loop, print how this affects the gravitational force. You only need to print this change for every 1000 km.
(15 points)
The Fibonacci sequence is a famous mathematical sequence of numbers that is strongly related to the Golden Ratio. One of the wonders of the sequence is its exceedingly simple definition. Let Fn define the nth number in the Fibonacci sequence, where F1 = 1 and F2 = 1. Then, the nth number can be computed as: Fn = Fn - 1 + Fn - 2
Create a file called bonus_question.py
. In this file,
write a function called compute_fib
, which takes a
single parameter n, and uses a for
loop to compute and ultimately return the nth number of
the Fibonacci sequence.
(10 points)
When you have finished, create a tar file of your test1
directory. To create a tar file, execute the following commands:
cd ~/cs120 tar czvf test1.tgz test1/
To submit your activity, go to cseval.roanoke.edu. You should
see an available assignment called Test 1
.