< Back

Lab 4: Errors and Debugging

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 lab4
$ cd lab4


Finding Your Pace

You might think that running has very little to do with Computer Science. However, there are a lot of useful computations we can do to help a runner figure out how fast they ran. Race distances are typically given in kilometers: 5k, 10k, etc. A fast runner might run a 5K in 20 minutes, but the winning time is usually closer to 15 minutes. In the US, runners typically measure their pace in minutes per mile. We will write a program to perform these computations.

Details

In Emacs, create a Python program in a file called runner.py. Your program should have variables that store:

Your program should then compute and print the users pace in minutes per mile. Make sure you use proper variable names, formats the output nicely, and follow the courses code conventions.

Example

$ python3 runner.py
If you run a 5 kilometer race in 27 minutes and 15 seconds,
your pace will be 8.72 minutes per mile.

Hint

  • Begin by defining the variables and constants. You will need variables for the distance in kilometers, distance in miles, minutes part of the time, seconds part of the time, and minutes per mile.
  • Then, compute the distance in miles and the time in minutes. Use the approximation that 5 kilometers is about 3.1 miles.
  • The pace of the runner is just their time divided by the distance of the race in miles.

Challenge

Many runners set goals to improve their pace. Add to your program a variable for the user to store their desired minutes per mile into. Compute how quickly they would have to run the race, and print this value as well.


Voyager 1

Voyager 1 is a space probe that was launched by NASA in 1977. It is currently the farthest man-made object from Earth. It is very likely that Voyager 1 is (or will be) the first man-made object to exit the solar system. Currently, Voyager 1 is approximately 19,279,655,290 km from Earth. The Voyager 1 communicates with radio waves, which travel at the speed of light, 299,792,458 m/s. At such a great distance, communications take an appreciable amount of time to reach earth. And yet, Voyager 1 is still closer to the Sun than any other star in our galaxy.

Details

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. The Voyager 1's velocity is 61,400 km/h. Proxima Centauri is 4.2 light years from Earth. These calculation should be performed using Python's arithmetic operations and printed nicely labeled and formatted. Be sure to use variables where appropriate, and follow the course code conventions.

Example

$ python3 voyager.py
It takes 64310.00772541116 seconds for a message from Voyager to reach Earth.
It will take 73789.26858417667 years for Voyager to reach Proxima Centauri.

Hint

  • You first need to figure out how many meters Voyager 1 is from Earth. Recall that there are 1000 meters in one kilometer.

  • The speed of light above is given in meters per second. So if you divide the distance in meters by the speed of light you will get how many seconds a signal takes to reach Earth.

  • One light year is the distance light travels in one year. You have the speed of light, so you simply need to multiply by the number of seconds in one year to compute this distance.

  • To get how long it will take Voyager to reach Proxima Centauri, you simply need to divide the distance in meters you computed previously by the speed of Voyager in meters per second. Don't forget to convert this into years!

Challenge

Voyager gets its power through the radioactive decay of plutonium-238. It is estimated that Voyager loses about 0.79% of its power output per year. Using a slightly modified version of the compound interest formula, compute how many years Voyager could maintain power using the built in power supply.

$$power\_end = start\_power \times (1 - decay\_rate) ^ {time}$$