Python Programs | IT Developer
IT Developer

Python Programs



Share with a Friend

Python Programs - Conditional Statements

Determine electricity bill category based on units consumed - Python Program

Example 1 : Basic Program

units = int(input("Enter units consumed: ")) if units <= 100: category = "Low" elif units <= 300: category = "Medium" else: category = "High" print("Category:", category)

Output

 
OUTPUT 1 :
Enter units consumed: 125
Category: Medium 

OUTPUT 2 :
Enter units consumed: 325
Category: High
  

Example 2 : Advanced Program

def calculate_electricity_bill(units): """ Calculates the electricity bill and determines the category based on units consumed. Args: units (int): The number of electricity units consumed. Returns: tuple: A tuple containing the bill amount (float) and the category (str). Returns (None, "Invalid input") if units are negative. """ if units < 0: return None, "Invalid input: Units consumed cannot be negative." bill_amount = 0.0 category = "" if 0 <= units <= 100: bill_amount = units * 3.00 category = "Residential - Low Consumption" elif 101 <= units <= 300: bill_amount = (100 * 3.00) + ((units - 100) * 5.00) category = "Residential - Medium Consumption" elif 301 <= units <= 500: bill_amount = (100 * 3.00) + (200 * 5.00) + ((units - 300) * 7.50) category = "Residential - High Consumption" else: # units > 500 bill_amount = (100 * 3.00) + (200 * 5.00) + (200 * 7.50) + ((units - 500) * 10.00) category = "Commercial / Very High Consumption" return bill_amount, category # Get units consumed from user try: units_consumed = int(input("Enter the number of units consumed: ")) bill, cat = calculate_electricity_bill(units_consumed) if bill is not None: print(f"\nUnits Consumed: {units_consumed}") print(f"Electricity Bill Category: {cat}") print(f"Total Electricity Bill: Rs. {bill:.2f}") else: print(cat) # Prints the error message for invalid input except ValueError: print("Invalid input: Please enter a whole number for units consumed.")

Output

 
OUTPUT 1 :
Enter the number of units consumed: 325

Units Consumed: 325
Electricity Bill Category: Residential - High Consumption
Total Electricity Bill: Rs. 1487.50 

OUTPUT 2 :
Enter the number of units consumed: 125

Units Consumed: 125
Electricity Bill Category: Residential - Medium Consumption
Total Electricity Bill: Rs. 425.00

OUTPUT 3 :
Enter the number of units consumed: 75

Units Consumed: 75
Electricity Bill Category: Residential - Low Consumption
Total Electricity Bill: Rs. 225.00
  

Explanation

calculate_electricity_bill(units) Function:

  • This function takes an integer unitsrepresenting the electricity units consumed as input.
  • It first checks if the input unitsare negative; if so, it returns an error message.
  • It then uses a series of if-elif-elsestatements to determine the electricity bill category and calculate the bill amount based on predefined tiers and rates:
    • 0-100 units:Charged at Rs. 3.00 per unit. Category: "Residential - Low Consumption".
    • 101-300 units:First 100 units at Rs. 3.00, remaining at Rs. 5.00 per unit. Category: "Residential - Medium Consumption".
    • 301-500 units:First 100 units at Rs. 3.00, next 200 at Rs. 5.00, remaining at Rs. 7.50 per unit. Category: "Residential - High Consumption".
    • Above 500 units:Rates apply progressively, with units above 500 charged at Rs. 10.00 per unit. Category: "Commercial / Very High Consumption".
  • The function returns a tuple containing the calculated bill_amountand the determined category.

User Input and Output:

  • The program prompts the user to enter the units_consumed.
  • It uses a try-exceptblock to handle potential ValueError if the user enters non-integer input.
  • It calls the calculate_electricity_billfunction to get the bill and category.
  • Finally, it prints the units consumed, the determined electricity bill category, and the total electricity bill amount, formatted to two decimal places.