用实例编写 Python示例计算电费。为此,我们使用了 Elif 语句。
这个 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 代码非常有用。类似于:如果你消耗 300 到 500 个单位,那么单位固定为 7.75 卢比,等等。让我们看看 python 程序
提示: Elif 语句先检查蟒的情况。如果为真,则执行该块中的语句。如果条件为假,则 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)
蟒电程序输出
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
标签: 兼职