我无法得到" totalBill"在我的代码的最后一行打印正确。我不确定如何让它实现用户输入,只在约束时使用它们。
# 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)
&#13;
答案 0 :(得分:0)
我注意到了几件事:
if begMeter > endMeter:
waterUsedTotal = (((999999999 - begMeter) + 1) + endMeter) /10
else:
waterUsedTotal = waterUsed * 0.1
在第一种情况下,waterUsedTotal是int
,也就是说,它是向下舍入的。在第二种情况下,它被转换为float
。这是不一致的。
更重要的是,但是:
if customerCode.lower() == 'r' or 'R':
此行始终为true,因为'R'
被解释为true。而其他情况为elif
,因此它们永远不会被执行。
我怀疑你想要的是:如果用户输入'r'
或'R'
,请执行此操作,否则如果用户输入了'c'
或'C'
那么,如果用户输入'i'
或'I'
做最后一件事。
我建议:
if customerCode in ['r', 'R']:
或
if customerCode.lower() == 'r':
(.lower()
意味着它永远不会是任何首都角色。)