编写一个 Python 程序 来计算电费,并附带一个示例。为此,我们使用 Elif 语句。
Python 编写电费计算示例 1
此 Python 程序 允许用户输入消耗的单位。接下来, Python 计算总电费。如果电力公司对不同单位收取不同的费率,则此方法很有用。在此示例中,我们使用 Elif 语句。
# Python Program to Calculate Electricity Bill
units = int(input(" Please enter Number of Units you Consumed : "))
if(units < 50):
amount = units * 2.60
surcharge = 25
elif(units <= 100):
amount = 130 + ((units - 50) * 3.25)
surcharge = 35
elif(units <= 200):
amount = 130 + 162.50 + ((units - 100) * 5.26)
surcharge = 45
else:
amount = 130 + 162.50 + 526 + ((units - 200) * 8.45)
surcharge = 75
total = amount + surcharge
print("\nElectricity Bill = %.2f" %total)

Python 编写电费计算示例 2
如果电力公司采用统一费率,此 Python 代码则很有用。例如:如果您消耗的单位在 300 到 500 之间,则每单位固定收费 7.75 卢比等。让我们看一下 Python 程序。
提示: Elif 语句会先检查第一个 Python 条件。如果为 TRUE,则执行该块中的语句。如果条件为 FALSE, Python 程序 将检查下一个(Elif 条件)依此类推。
# Python Program to Calculate Electricity Bill
units = int(input(" Please enter Number of Units you Consumed : "))
if(units > 500):
amount = units * 9.65
surcharge = 85
elif(units >= 300):
amount = units * 7.75
surcharge = 75
elif(units >= 200):
amount = units * 5.26
surcharge = 55
elif(units >= 100):
amount = units * 3.76
surcharge = 35
else:
amount = units * 2.25
surcharge = 25
total = amount + surcharge
print("\nElectricity Bill = %.2f" %total)
Python 电费计算程序输出
Please enter Number of Units you Consumed : 450
Electricity Bill = 3562.50
>>>
Please enter Number of Units you Consumed : 750
Electricity Bill = 7322.50
>>>
Please enter Number of Units you Consumed : 250
Electricity Bill = 1370.00
>>>
Please enter Number of Units you Consumed : 150
Electricity Bill = 599.00
>>>
Please enter Number of Units you Consumed : 50
Electricity Bill = 137.50