generate electricity bill in python program

 def calculate_electricity_bill(units):

    """

    Calculates the electricity bill based on tiered unit charges.


    Args:

        units (float): The total units of electricity consumed.


    Returns:

        float: The total bill amount.

    """

    total_bill = 0.0

    

    # Define the tariff rates (example rates)

    # First 100 units: $0.10 per unit

    # Next 100 units (101-200): $0.15 per unit

    # Next 100 units (201-300): $0.20 per unit

    # Above 300 units: $0.25 per unit


    if units <= 100:

        total_bill = units * 0.10

    elif units <= 200:

        # Bill for first 100 units + remaining units at the next rate

        total_bill = (100 * 0.10) + ((units - 100) * 0.15)

    elif units <= 300:

        # Bill for first 100 + next 100 + remaining units at the next rate

        total_bill = (100 * 0.10) + (100 * 0.15) + ((units - 200) * 0.20)

    else:

        # Bill for first 100 + next 100 + next 100 + remaining units at the highest rate

        total_bill = (100 * 0.10) + (100 * 0.15) + (100 * 0.20) + ((units - 300) * 0.25)

        # Optional: Add a surcharge for high consumption, e.g., 2.5%

        # total_bill = total_bill * 1.025


    return total_bill


# Main part of the program

if __name__ == "__main__":

    try:

        # Prompt the user to enter the number of units consumed

        units_consumed_input = input("Enter the number of electricity units consumed: ")

        units_consumed = float(units_consumed_input)


        if units_consumed < 0:

            print("Units consumed cannot be negative. Please enter a valid number.")

        else:

            # Calculate the bill

            bill_amount = calculate_electricity_bill(units_consumed)


            # Display the result

            print(f"\n--- Electricity Bill Details ---")

            print(f"Units Consumed: {units_consumed:.2f}")

            print(f"Total Amount to Pay: ${bill_amount:.2f}")

            print(f"--------------------------------")

            

    except ValueError:

        print("Invalid input. Please enter a numerical value for units consumed.")



Comments