Answer :
To solve the problem of calculating the total electricity bill based on the given consumption, we need to carefully analyze the tariff structure and develop a program that computes the bill according to the specified rates.
### Step-by-Step Solution
1. Understand the Tariff Structure:
- For consumption between `0-60 kWh/month`, there are distinct tariffs for the first `30 kWh` and the next `30 kWh`.
- For consumption above `60 kWh/month`, different ranges are specified with their own rates.
2. Construct a Logic to Calculate the Bill:
- Based on the consumption input, identify the appropriate range and compute the energy charge (rate per kWh) and fixed charge (LKR/month) accordingly.
3. Prompt the User for Input:
- Use the `input()` function to get the user's electricity consumption.
4. Calculate the Bill:
- Use conditionals to check which slab the consumption falls into and compute the total bill by adding the energy charge and fixed charge.
Here is the detailed Python program to compute the electricity bill:
```python
def calculate_electricity_bill():
consumption = float(input("Enter your electricity consumption in kWh: "))
# Define the tariff structure
tariff_structure = [
{"range": (0, 30), "charge": 25.00, "fixed": 300.00},
{"range": (31, 60), "charge": 32.00, "fixed": 4.00},
{"range": (0, 60), "charge": 10.00, "fixed": 150.00},
{"range": (61, 90), "charge": 50.00, "fixed": 1500.00},
{"range": (91, 120), "charge": 75.00, "fixed": 2000.00},
{"range": (121, 180), "charge": 50.00, "fixed": 1000.00},
{"range": (181, float('inf')), "charge": 35.00, "fixed": 400.00}
]
# Initialize the charges
energy_charge = 0
fixed_charge = 0
# Find and compute the appropriate charges
if consumption <= 60:
if consumption <= 30:
energy_charge = consumption 25.00
fixed_charge = 300.00
else:
energy_charge = 30 25.00 + (consumption - 30) 32.00
fixed_charge = 300.00 + 4.00
else:
# Loop through each tariff and apply if range is matched
for tariff in tariff_structure:
if tariff["range"][0] <= consumption <= tariff["range"][1]:
energy_charge = consumption tariff["charge"]
fixed_charge = tariff["fixed"]
break
total_bill = energy_charge + fixed_charge
print("Energy Charge (LKR):", energy_charge)
print("Fixed Charge (LKR):", fixed_charge)
print("Total Electricity Bill (LKR):", total_bill)
# Execute the function
calculate_electricity_bill()
```
### Explanation
1. Input: The program asks the user to input their electricity consumption in kWh.
2. Tariff Initialization: The tariff structure is defined in a list, where each dictionary contains the range of consumption, charge per kWh, and fixed monthly charge.
3. Bill Calculation:
- For consumption up to 60 kWh, the program computes charges for the respective slabs, with separate calculations for `0-30 kWh` and `31-60 kWh`.
- For consumption above 60 kWh, it iterates through the tariff structure to find the matching range and applies the corresponding rates.
4. Output: It prints out the energy charge, fixed charge, and total electricity bill.
This structured approach ensures that the program computes the electricity bill accurately based on the given tariff structure.
### Step-by-Step Solution
1. Understand the Tariff Structure:
- For consumption between `0-60 kWh/month`, there are distinct tariffs for the first `30 kWh` and the next `30 kWh`.
- For consumption above `60 kWh/month`, different ranges are specified with their own rates.
2. Construct a Logic to Calculate the Bill:
- Based on the consumption input, identify the appropriate range and compute the energy charge (rate per kWh) and fixed charge (LKR/month) accordingly.
3. Prompt the User for Input:
- Use the `input()` function to get the user's electricity consumption.
4. Calculate the Bill:
- Use conditionals to check which slab the consumption falls into and compute the total bill by adding the energy charge and fixed charge.
Here is the detailed Python program to compute the electricity bill:
```python
def calculate_electricity_bill():
consumption = float(input("Enter your electricity consumption in kWh: "))
# Define the tariff structure
tariff_structure = [
{"range": (0, 30), "charge": 25.00, "fixed": 300.00},
{"range": (31, 60), "charge": 32.00, "fixed": 4.00},
{"range": (0, 60), "charge": 10.00, "fixed": 150.00},
{"range": (61, 90), "charge": 50.00, "fixed": 1500.00},
{"range": (91, 120), "charge": 75.00, "fixed": 2000.00},
{"range": (121, 180), "charge": 50.00, "fixed": 1000.00},
{"range": (181, float('inf')), "charge": 35.00, "fixed": 400.00}
]
# Initialize the charges
energy_charge = 0
fixed_charge = 0
# Find and compute the appropriate charges
if consumption <= 60:
if consumption <= 30:
energy_charge = consumption 25.00
fixed_charge = 300.00
else:
energy_charge = 30 25.00 + (consumption - 30) 32.00
fixed_charge = 300.00 + 4.00
else:
# Loop through each tariff and apply if range is matched
for tariff in tariff_structure:
if tariff["range"][0] <= consumption <= tariff["range"][1]:
energy_charge = consumption tariff["charge"]
fixed_charge = tariff["fixed"]
break
total_bill = energy_charge + fixed_charge
print("Energy Charge (LKR):", energy_charge)
print("Fixed Charge (LKR):", fixed_charge)
print("Total Electricity Bill (LKR):", total_bill)
# Execute the function
calculate_electricity_bill()
```
### Explanation
1. Input: The program asks the user to input their electricity consumption in kWh.
2. Tariff Initialization: The tariff structure is defined in a list, where each dictionary contains the range of consumption, charge per kWh, and fixed monthly charge.
3. Bill Calculation:
- For consumption up to 60 kWh, the program computes charges for the respective slabs, with separate calculations for `0-30 kWh` and `31-60 kWh`.
- For consumption above 60 kWh, it iterates through the tariff structure to find the matching range and applies the corresponding rates.
4. Output: It prints out the energy charge, fixed charge, and total electricity bill.
This structured approach ensures that the program computes the electricity bill accurately based on the given tariff structure.