Debugging a Telescope

Start by watching the Debugging short for CS50

CS50 YouTube Link

Debugging

CS50 Week 0 Shorts

  1. Visual Studio Code for CS50
  2. Functions
  3. Return Values
  4. Side Effects
  5. Variables

Background

One of the tools beloved by scientists of all kinds is the telescope. The concept of magnifying with glass lenses, both for extremely small things close by and extremely large things very far away, has been an integral part of studying nature from every angle we can.

Today we will be studying the concept of scope in programming with the telescope of a debugger!

Understanding

A telescope's magnification increases the size of everything in it's field of view by a factor set by the dial. Assume that we have a telescope set up so that at x1 zoom (no magnification), an object that is 1 meter tall and 1 meter away from the telescope fills the view. Suppose we see a friend in the distance who is 1.77 meters tall, and we want to know how far away our friend is. We can calculate it using this relationship with our telescope!

First, we turn the dial until our friend fills up our entire view (their head is at the top and their feet are at the bottom). The formula for that distance is:

\[ D = \text{Object Height} \times \frac{\text{Base Height = 1 meter}}{\text{Base Distance = 1 meter}} \times Magnification \]

For example, if our 1.77 m friend fills the view at x2 zoom, our friend must be 3.54 meters away.

We have implemented a program to calculate this distance, given the height of an object (or friend) and the zoom setting it took to make them fill up the view.

There is just one problem. It has bugs! Use the VSCode debugger to fix the bugs and make the program work properly.

Before You Begin

Turn off all LSPs, Linters, and Autocompletion help if using a local IDE. We strongly recommend using the Codespace and/or disabling any extra tools for this exercise to make the most of the opportunity.

Execute cd by itself in your terminal window. You should find that your terminal window’s prompt resembles the below:

1
$
Next execute
1
mkdir -p debugging/telescope
to make a folder called telescope inside of another folder called debugging in your codespace. (debugging will be created if it does not already exist.)

Then execute

1
cd debugging/telescope
to change directories into that folder.

You should now see your terminal prompt as debugging/telescope/ . You can now execute

1
wget https://raw.githubusercontent.com/alum-challenges/problems/main/python/week-0/debugging/telescope/telescope.py
to download the bugged program. Then execute
1
code telescope.py
to open the new
telescope.py` where you will debug the existing program.

Specification

The resulting program should implement a function called zoom which accepts two floats (the size of an object and the magnification level of the telescope), and returns the distance to the object as a float.

The program should prompt the user for the magnification level on the dial and the size of the object in meters. It should then calculate the distance and print it as The object is {value} meters away. The output should be accurate to 2 decimal places.

Hints

More about functions: https://docs.python.org/3/tutorial/controlflow.html#defining-functions

How to Test

If you run into an error saying your file cannot be opened, retrace your steps to be sure that you are inside your telescope folder and have saved your telescope.py file there.

You can execute the below to check your code using check50, a program that CS50 will use to test your code:

1
check50 alum-challenges/problems/main/python/week-0/debugging/telescope/tests
* Green smiles mean your program has passed a test! * Red frowns will indicate your program output something unexpected. * Orange neutral faces mean you must fix the failed check before those checks can run.

Visit the URL that check50 outputs to see the input check50 handed to your program, what output it expected, and what output your program actually gave.

How to Submit

Coming soon