2013-10-10 63 views
0

我得到「R,B,C和P」爲我的輸出時,它應該是「住宅,商業,城市和教區」Python3 if語句在循環

我也越來越0.00我總的來說,如果它的業務,城市或教區(他們都不同),我需要總數......這不是計算。

我從.data文件讓我的投入,他們是正確的

print("=========================================================") 
print(format("Name", '<12s'),format("Type", '<15s'),format("Location", '<10s'),format("KwH", '>6s'),format("Total", '>10s')) 
print("=========================================================") 
total = 0 
for i in range(10): 
    custName = input() 
    custType = input() 
    custLoc = input() 
    custKwh = eval(input()) 
    if (custType == "R"): 
     custType = "Residential" 
    if (custType == "B"): 
     custType = "Business" 
     total = (custKwh * 0.05710) + 10 
    if (custLoc == "C"): 
     custLoc = "City" 
     total = (custKwh * 0.0401) + 6 
    if (custLoc == "P"): 
     custLoc = "Parish" 
     total = (custKwh * 0.04411) + 6.60 
    print(format(custName, '<12s'),format(custType, '<15s'),format(custLoc, '<10s'),format(custKwh, '>6d'),format(total, '>10.2f')) 

輸入是:

Smith R P 4500 Taylor R C 6000 Williams B C 10500 Johnson R C 7500 Davis R P 3000 Woods B P 25300 Morgan R C 5800 Landry R C 3900 Young B P 18500 Wilson R P 7000 
+0

首先,我不明白你是如何得到任何輸出,因爲你不輸出任何東西。其次,也許你應該在更新代碼後顯示你的輸入例子。 – sberry

+0

代碼已更新,這是我的整個代碼。 – user2865217

+0

看起來像您的輸入需要您在每個值之後按enter鍵,它都會放入custName變量中。你可以通過使用.split('') – ranman

回答

1

我將它改寫爲這樣的:

print("=========================================================") 
print(
    format("Name", '<12s'), 
    format("Type", '<15s'), 
    format("Location", '<10s'), 
    format("KwH", '>6s'), 
    format("Total", '>10s') 
) 
print("=========================================================") 
total = 0 
for i in range(10): 
    custName, custType, custLoc, custKwh = input().split(' ') 
    custKwh = int(custKwh) 
    if (custType == "R"): 
     custType = "Residential" 
    if (custType == "B"): 
     custType = "Business" 
     total = (custKwh * 0.05710) + 10 
    if (custLoc == "C"): 
     custLoc = "City" 
     total = (custKwh * 0.0401) + 6 
    if (custLoc == "P"): 
     custLoc = "Parish" 
     total = (custKwh * 0.04411) + 6.60 
    print(
     format(custName, '<12s'), 
     format(custType, '<15s'), 
     format(custLoc, '<10s'), 
     format(custKwh, '>6d'), 
     format(total, '>10.2f') 
    ) 

的這裏的關鍵錯誤是您在檢查custLoc時複製custType(複製粘貼錯誤?)

+0

this仍然沒有解決我的問題,仍然得到R,B,C和P .....並且總值仍然是0.00。我應該設置total =還是別的嗎?爲什麼它沒有計算 – user2865217

+0

粘貼你正在運行的確切命令 – ranman

+0

你能聊天嗎? – user2865217