Problem 145385 · medium · Phase 01 Prerequisites & Setup

Tiered Electricity Bill

conditionals · arithmetic

A utility bills electricity in tiers. Write electricity_bill(kwh) that returns the amount due as a float rounded to 2 decimals:

  • the first 100 kWh cost 0.10 each
  • the next 200 kWh (from 100 up to 300) cost 0.15 each
  • every kWh above 300 costs 0.25
  • a fixed service charge of 5.00 is added whenever any electricity was used at all; a customer who used nothing pays 0.0

Examples

Input:  kwh = 0
Output: 0.0

Input:  kwh = 100
Output: 15.0
Explanation: 100 * 0.10 = 10.0, plus the 5.00 service charge.

Input:  kwh = 350
Output: 57.5
Explanation: 5 + 10 (first 100) + 30 (next 200) + 12.5 (50 at 0.25).

Constraints

  • 0 <= kwh <= 10**6

Goals

  • Charge each block of usage at its own rate
  • Add a fixed charge only when there is any usage
  • Round money to two decimals
Starting Python…