【发布时间】:2015-05-11 11:05:39
【问题描述】:
我无法在代码的最后一行正确打印“totalBill”。我不确定如何让它实现用户输入,并且只使用那些 if 约束。
# Compute and display the customer bill for water usage during the billing period
# Prompt the user to enter 3 values:
# a. The customers code (a character)
# b. The customer's beginning meter reading (a positive integer value)
# c. The customer's ending meter reading (a positive integer value)
# Process info and display results
customerCode = str(input("Please enter your customer code: "))
begMeter = int(input("Please enter your beginning meter reading: "))
endMeter = int(input("Please enter your ending meter reading: "))
waterUsed = endMeter - begMeter
if begMeter > endMeter:
waterUsedTotal = (((999999999 - begMeter) + 1) + endMeter) /10
else:
waterUsedTotal = waterUsed * 0.1
a = 4000000
b = 0.00025
d = 10000000
e = 0.0005
if customerCode.lower() == 'r' or 'R':
totalBill = (5.00 + (waterUsedTotal * e))
elif customerCode.lower() == 'c' or 'C':
if waterUsed <= a:
totalBill = 1000.00
elif waterUsed > a:
totalBill = (1000.00 + (waterUsedTotal * b))
elif customerCode.lower() == 'i' or 'I':
if waterUsedTotal <= a:
totalBill = 1000.00
elif waterUsed > a and waterUsed <= d:
totalBill = 2000.00
elif waterUsed > d:
totalBill = 2000.00 + (waterUsedTotal * b)
print("")
print("Customer code: ", customerCode)
print("Beginning meter reading: ", begMeter)
print("Ending meter reading: ", endMeter)
print("Gallons of water used: ", "%.1f" % waterUsedTotal)
print("Amount billed: $", "%.2f" % totalBill)
【问题讨论】:
-
“正确打印” - 这是什么意思?它应该做什么?它有什么作用?
-
我建议在每个 'if..elif' 块结束时加上最后的 'else' 以捕获所有其他情况,您可能会错过任何 , 或 at 未处理的边缘情况使用默认值最少初始化“totalBill”
-
请不要删除您发布的代码;没有它,答案(你认为它是正确的)毫无意义。
标签: python loops if-statement