Bug Hunt

The questions below are due on Friday June 14, 2024; 10:00:00 PM.
 
You are not logged in.

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 code block that doesn't quite succeed in all cases. Try running the code block on your own machine with different inputs to see where the code doesn't behave properly and describe the bug.

2) Part 1

Consider the following code, which is designed to convert a temperature in degrees Celsius to a temperature in degrees Fahrenheit and print the result:

# Inputs:
celsius = 27

print(celisus * 1.8 + 32)

We want the above code to work for any value of celsius, even if that value is changed. Are there any bugs in the code? If so, briefly explain them:

Formatting Help

3) Part 2

Consider the following code, which is designed to calculate the amount in a bank account with an annual interest rate r (compounded annually) after n years, with an initial investment of d dollars, and print the result:

# Inputs:
r = .06  # interest rate
d = 100  # current number of dollars in the bank account
n = 20  # years

# calculate the amount of dollars after adding interest
(d * (1+r)**n)

print(d)

We want the above code to work for any value of r, d, and n, even if those values are changed. Are there any bugs in the code? If so, briefly explain them:

Formatting Help

4) Part 3

Consider the following code, which is designed "clip" a value x to be between two limiting values, lo and hi

It should print:

  • the value of x if x is between lo and hi,
  • the value of lo if x is less than lo, or
  • the value of hi if x is greater than hi

The code is as follows:

# Inputs:
x = 20
lo = 6
hi = 9

if x < lo:
    print(lo)
if x > hi:
    print(hi)
else:
    print(x)

We want the above code to work for any values of x, lo, and hi, even if those values change. Are there any bugs in the code above? If so, briefly explain them:

Formatting Help

Next Exercise: Survey

Back to exercises