Introduction:
In this program, the calculate_electricity_bill() function takes the number of units consumed as an input and returns the corresponding cost. The program then prompts the user to enter the number of units consumed, calculates the bill using the function, and displays the result.
Algorithm for calculate_electricity_bill(units):
- Start the function calculate_electricity_bill with the input parameter units.
- If units is less than or equal to 100, set cost to 0.0.
- If units is greater than 100 and less than or equal to 200, calculate the cost by subtracting 100 from units and multiplying the result by 2.50. Assign this value to cost.
- If units is greater than 200 and less than or equal to 300, calculate the cost as follows:
- Set cost to the cost of the first 100 units (100 * 2.50).
- Add the cost of the remaining units (i.e., (units – 200) * 3.00) to cost.
- If units is greater than 300 and less than or equal to 400, calculate the cost as follows:
- Set cost to the cost of the first 100 units (100 * 2.50).
- Add the cost of the next 100 units (100 * 3.00) to cost.
- Add the cost of the remaining units (i.e., (units – 300) * 5.00) to cost.
- If units is greater than 400, calculate the cost as follows:
- Set cost to the cost of the first 100 units (100 * 2.50).
- Add the cost of the next 100 units (100 * 3.00).
- Add the cost of the next 100 units (100 * 5.00).
- Add the cost of the remaining units (i.e., (units – 400) * 6.00) to cost.
- Return the final value of cost.
Code:
def calculate_electricity_bill(units):
if units <= 100:
cost = 0.0
elif units <= 200:
cost = (units - 100) * 2.50
elif units <= 300:
cost = 100 * 2.50 + (units - 200) * 3.00
elif units <= 400:
cost = 100 * 2.50 + 100 * 3.00 + (units - 300) * 5.00
else:
cost = 100 * 2.50 + 100 * 3.00 + 100 * 5.00 + (units - 400) * 6.00
return cost
units = int(input("Enter the number of units consumed: "))
bill = calculate_electricity_bill(units)
print(f"Electricity Bill: {bill:.2f} Rupees")
Input:
Enter the number of units consumed: 600
Output:
Electricity Bill: 2250.00 Rupees
Explanation:
The function calculate_electricity_bill(units) calculates the electricity bill based on the number of units consumed. It follows a specific set of conditions to determine the cost:
- If the number of units consumed is less than or equal to 100, the cost is set to 0.0.
- If the number of units consumed is between 101 and 200 (inclusive), the cost is calculated by subtracting 100 from the units consumed and multiplying the result by 2.50.
- If the number of units consumed is between 201 and 300 (inclusive), the cost is calculated as follows:
- The cost for the first 100 units is calculated as 100 * 2.50.
- The cost for the remaining units is calculated by multiplying the difference between the units consumed and 200 by 3.00, and then adding it to the previous cost.
- If the number of units consumed is between 301 and 400 (inclusive), the cost is calculated as follows:
- The cost for the first 100 units is calculated as 100 * 2.50.
- The cost for the next 100 units is calculated as 100 * 3.00.
- The cost for the remaining units is calculated by multiplying the difference between the units consumed and 300 by 5.00, and then adding it to the previous cost.
- If the number of units consumed is greater than 400, the cost is calculated as follows:
- The cost for the first 100 units is calculated as 100 * 2.50.
- The cost for the next 100 units is calculated as 100 * 3.00.
- The cost for the next 100 units is calculated as 100 * 5.00.
- The cost for the remaining units is calculated by multiplying the difference between the units consumed and 400 by 6.00, and then adding it to the previous cost.
Finally, the program prompts the user to enter the number of units consumed, calls the calculate_electricity_bill function with the input value, and displays the calculated electricity bill in Rupees, formatted to two decimal places.