Bug Hunt
Please Log In for full access to the web site.
Note that this link will take you to an external site (https://shimmer.mit.edu) to authenticate, and then you will be redirected back to this page.
1) Instructions
For the following questions, we specify the behavior the code should perform and give an example program which contains one or more bugs. Try running the program on your own machine with different inputs to see where the code doesn't behave properly. For each bug, specify its type (Syntax, Runtime, or Semantic), its effect, and one or more potential ways to fix it.For example, the absolute_value
function in the reading has a semantic bug which causes all non-zero numbers to become negative instead of positive. This bug could be fixed
by changing the conditional statement to if x < 0:
or by returning -x
within the else statement instead of in the if statement.
2) Part 1
Consider the following program, which is designed to convert a temperature in degrees Celsius to a temperature in degrees Fahrenheit. You may assume that the input is always a number at or above -273.15 °C.
def celsius_to_fahrenheit(celisus):
return celsius * 1.8 + 32
print(celsius_to_fahrenheit(27))
Formatting Help
3) Part 2
Consider the following program, which is designed to calculate the amount in a bank account with an annual interest rate
(compounded annually) after num_years
, with an initial investment of dollars
, and return the result. You may assume that the inputs are always non-negative numbers, and that the interest
rate is a float between 0.0-1.0.
def calc_interest(dollars, rate, num_years)
return dollars * (1 + rate) ** num_years)
print(calc_interest(100, .06, 20))
Formatting Help
4) Part 3
Consider the following program, which is designed "clip" a value val
to be between two limiting values, lo
and hi
and print the result.
It should print:
- the value of
val
ifval
is betweenlo
andhi
, - the value of
lo
ifval
is less thanlo
, or - the value of
hi
ifval
is greater thanhi
You may assume that the three inputs are always numbers, and that lo < hi
.
def clip(val, lo, hi):
if val < lo:
print(lo)
if val > hi:
print(hi)
else:
print(val)
clip(20, 6, 9)
Formatting Help
Next Exercise: Survey