Created
June 23, 2024 11:45
-
-
Save nfl0/0ddd5a1c9ee7f4d010216e2b9951961c to your computer and use it in GitHub Desktop.
ONE electric bill calculator; tiers table reference: https://web.archive.org/web/20240623104347/http://www.one.org.ma/FR/pages/interne.asp?esp=1&id1=3&id2=45&id3=25&t2=1&t3=1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def calculate_electricity_bill(consumption_kwh): | |
# Define price tiers in MAD/kWh | |
price_tiers = [ | |
(100, 0.9010), | |
(50, 1.0732), | |
(50, 1.0732), | |
(100, 1.1676), | |
(200, 1.3817), | |
(float('inf'), 1.5958) | |
] | |
# Calculate the bill based on the consumption tiers | |
remaining_consumption = consumption_kwh | |
total_cost = 0.0 | |
for limit, price_per_kwh in price_tiers: | |
if remaining_consumption > limit: | |
total_cost += limit * price_per_kwh | |
remaining_consumption -= limit | |
else: | |
total_cost += remaining_consumption * price_per_kwh | |
break | |
return total_cost | |
def calculate_total_cost(power_consumption_w, daily_usage_hours, days_in_period): | |
# Daily consumption in kWh | |
daily_consumption_kwh = (power_consumption_w / 1000) * daily_usage_hours | |
# Monthly consumption in kWh for the entire period | |
total_consumption_kwh = daily_consumption_kwh * days_in_period | |
# Fixed charges per month | |
fixed_meter_charge = 10.61 | |
fixed_maintenance_charge = 9.43 | |
fixed_monthly_charge = fixed_meter_charge + fixed_maintenance_charge | |
# Calculate total cost | |
total_cost_mad = calculate_electricity_bill(total_consumption_kwh) + fixed_monthly_charge | |
return total_cost_mad | |
def print_price_tiers(): | |
# Define price tiers in a readable format | |
tiers = [ | |
("0 - 100 kWh/month", "0.9010 MAD/kWh"), | |
("101 - 150 kWh/month", "1.0732 MAD/kWh"), | |
("151 - 200 kWh/month", "1.0732 MAD/kWh"), | |
("201 - 300 kWh/month", "1.1676 MAD/kWh"), | |
("301 - 500 kWh/month", "1.3817 MAD/kWh"), | |
("> 500 kWh/month", "1.5958 MAD/kWh") | |
] | |
print("\nPrice Tiers (MAD/kWh):") | |
for tier, price in tiers: | |
print(f"{tier}: {price}") | |
print() | |
def main(): | |
# User inputs | |
power_consumption_w = float(input("Enter the power consumption of the device in watts: ")) | |
daily_usage_hours = float(input("Enter the number of hours the device runs per day: ")) | |
# Total number of days in a month | |
days_in_period = 31 | |
# Calculate total electricity cost for the period | |
total_cost_mad = calculate_total_cost(power_consumption_w, daily_usage_hours, days_in_period) | |
# Calculate daily and monthly consumption | |
daily_consumption_kwh = (power_consumption_w / 1000) * daily_usage_hours | |
monthly_consumption_kwh = daily_consumption_kwh * days_in_period | |
# Print total cost for the period | |
print(f"\nTotal electricity cost for {days_in_period} days: {total_cost_mad:.2f} MAD\n") | |
# Print daily and monthly consumption | |
print(f"Daily power consumption: {daily_consumption_kwh:.2f} kWh") | |
print(f"Monthly power consumption: {monthly_consumption_kwh:.2f} kWh") | |
# Print price tiers | |
print_price_tiers() | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment