Debugging a Telescope
Start by watching the Debugging short for CS50
CS50 Week 0 Shorts
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:
telescope
inside of another folder called debugging
in your codespace. (debugging
will be created if it does not already exist.)
Then execute
to change directories into that folder.You should now see your terminal prompt as debugging/telescope/ . You can now execute
telescope.py` where you will debug the existing program.1
Specification
The resulting program should implement a function called zoom
which accepts two float
s (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
- Run your program with
python telescope.py
. Type5
and press Enter. At the next prompt, type3
and press Enter. Your program should outputThe object is 15.00 meters away
. - Run your program with
python telescope.py
. Type1
and press Enter. At the next prompt, type1
and press Enter. Your program should outputThe object is 1.00 meters away
. - Run your program with
python telescope.py
. Type1.77
and press Enter. At the next prompt, type3.5
and press Enter. Your program should outputThe object is 6.20 meters away
. - Run your program with
python telescope.py
. Type1.225
and press Enter. At the next prompt, type1
and press Enter. Your program should outputThe object is 1.23 meters away
.
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:
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