Founding Batch Offer · HLD · LLD · System Design at ₹799 until 30 Nov

Electricity Bill Calculation

Calculate a bill with increasing unit-price slabs in Python. Students trace cumulative charges, subtract already billed units, and test exact slab boundaries.

IntermediatePhase 1 - Level 4: Logical Operators & Compound StatementsExample 9 of 10
electricity-bill-calculation.py
1# Take units
2units = float(input("Enter units consumed: "))
3
4# Calculate bill based on slabs
5if units <= 100:
6 bill = units * 5
7elif units <= 200:
8 bill = 100 * 5 + (units - 100) * 7
9elif units <= 300:
10 bill = 100 * 5 + 100 * 7 + (units - 200) * 10
11else:
12 bill = 100 * 5 + 100 * 7 + 100 * 10 + (units - 300) * 15
13
14print(f"Total bill: ₹{bill}")

Output

Enter units consumed: 150
Total bill: ₹850.0

Enter units consumed: 250
Total bill: ₹1700.0

In this one

  • Calculate progressive slabs
  • Retain earlier block prices
  • Charge only excess units

How it works

  1. 1

    Read consumed units.

  2. 2

    Choose the slab and add completed lower-slab charges.

  3. 3

    Multiply remaining units by that slab's rate and print.

Edge cases

What is charged for 100 units?

What is charged for 100 units?

500, with every unit still in the first slab.

Are negative units rejected?

Are negative units rejected?

No. The first branch produces a negative bill; real billing input needs validation.

What's going on

A slab bill does not charge every unit at the highest rate reached. Earlier blocks keep their original prices; only the units beyond each cutoff use the next price.

For 150 units, the first hundred cost 500 and the next fifty cost 350, totaling 850. For 250, the total is 500 + 700 + 500 = 1700. Each branch includes complete earlier slabs before charging the remaining units.

What is charged for 100 units? 500, with every unit still in the first slab.

Are negative units rejected? No. The first branch produces a negative bill; real billing input needs validation.

Quiz

Tap an option. The answer stays hidden until you pick.

Question 1

How are 250 units billed?

Pick an answer to check it.

Question 2

What is charged for 100 units?

Pick an answer to check it.

Questions people ask

What is charged for 100 units?

500, with every unit still in the first slab.

Are negative units rejected?

No. The first branch produces a negative bill; real billing input needs validation.

How are 250 units billed?

1700.