In this post, we will write a Python program to calculate electricity bill by taking input from the user and also we will write this program by using a user-defined function. But Before we begin, we should know about electric charges and their rates.
Electricity Charges and their rates:
- 1 to 100 units – 1.5Rs
- 101 to 200 units – 2.5Rs
- 201 to 300 units – 4Rs
- 300 to 350 units – 5Rs
- Above 350 – Fixed charge 1500Rs
Now we know electricity charges and their rates, So now let’s start writing our program.
![Calculate Electricity Bill in Python](https://allinpython.com/wp-content/uploads/2023/05/Calculate-Electricity-Bill-in-Python-1024x392.jpg)
Calculate Electricity Bill in Python by Taking Input from the User
Few programming concepts you have to know before writing this program:
Source Code
# 1 to 100 units - 1.5Rs
# 101 to 200 units - 2.5Rs
# 201 to 300 units - 4Rs
# 300 to 350 units - 5Rs
# Above 350 - Fixed charge 1500Rs
units = float(input("Enter the Unit Consumed: "))
if units > 0 and units <= 100:
payment = units * 1.5
fixedCharge = 25 # Extra charge
elif units > 100 and units <= 200:
payment = (100 * 1.5) + (units - 100) * 2.5
fixedCharge = 50 # Extra charge
elif units > 200 and units <= 300:
payment = (100 * 1.5) + (200 - 100) * 2.5 + (units - 200) * 4
fixedCharge = 75 # Extra charge
elif units > 300 and units <= 350:
payment = (100 * 1.5) + (200 - 100) * 2.5 + (300 - 200) * 4 + (units - 300) * 5
fixedCharge = 100 # Extra charge
else:
payment = 0
fixedCharge = 1500
total = payment + fixedCharge
print(f"Your Electricity Bill amount is {total:.2f}")
Output-1
Enter the Unit Consumed: 275
Your Electricity Bill amount is 775.00
Output-2
Enter the Unit Consumed: 100
Your Electricity Bill amount is 175.00
Calculate Electricity Bill in Python Using Function
Few programming concepts you have to know before writing this program
- How to take input from the user
- Arithmetic Operator
- if-else &
- if-elif-else in python
- Python functions
Source Code
#function defination
def ElectricBillCal(units):
if units > 0 and units <= 100:
payment = units * 1.5
fixedCharge = 25 # Extra charge
elif units > 100 and units <= 200:
payment = (100 * 1.5) + (units - 100) * 2.5
fixedCharge = 50 # Extra charge
elif units > 200 and units <= 300:
payment = (100 * 1.5) + (200 - 100) * 2.5 + (units - 200) * 4
fixedCharge = 75 # Extra charge
elif units > 300 and units <= 350:
payment = (100 * 1.5) + (200 - 100) * 2.5 + (300 - 200) * 4 + (units - 300) * 5
fixedCharge = 100 # Extra charge
else:
payment = 0
fixedCharge = 1500
return payment + fixedCharge
u = float(input("Enter the Unit that you will Consumed: "))
totalBill = ElectricBillCal(u) # call a function
print(f"Your electricity bill: {totalBill:.2f}")
Output-1
Enter the Unit that you will Consumed: 201
Your electricity bill: 479.00
Output-2
Enter the Unit that you will Consumed: 400
Your electricity bill: 1500.00