Electric Bill

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.

Description

An electric company charges for monthly electricity usage each month based on usage, according to the following rules:

  1. Each of the first 500 kilowatt-hours used costs $0.50 / kWh
  2. The next 1000 kilowatt-hours used cost $0.72 / kWh
  3. The next 1000 kilowatt-hours used cost $1.16 / kWh
  4. Every kilowatt-hour beyond 2500 costs $1.98 / kWh

In addition, an additional fee of 20 dollars is always added to the bill. For an example, if you use 50 kwh, then the charges should be 0.50 * 50 + 20 = 45.00.

Write a program that computes the total amount owed, in dollars, at the end of a month, based on the number of kilowatt-hours used.

def calculate_bill(kwh_used):
    # calculate and return a final electric bill based on number of kwh_used

    pass # replace this line and start your code here

if __name__ == "__main__":
    # Testing: insert your test cases here
    print("Example test case: 250 kwh_used expected bill: __")
    calculate_bill(250)

Link to starter code file

Your program should return a single number representing the total amount owed (including the surcharge). Even though American currency doesn't quite work this way, you don't need to worry about rounding your result to the nearest cent (i.e. fractional cents are okay).

Notes

Testing

Make sure to write and test your code by hand before using Python. Draw environment diagrams for a few different test cases (one with less than 500 kWh used, one between 500 and 1500 kWh, one between 1500 and 2500 kWh, and one about 2500 kWh).

Then run your code with Python on your own machine as a test.

Submission

When you are ready (once you have simulated by hand and are convinced that your program will do the right thing), upload your file below for testing:

  No file selected

Next Exercise: Bug Hunt

Back to exercises