0

「住宅、ビジネス、都市、および教区」である必要があるときに、出力に「R、B、C、および P」を取得しています。

また、合計で 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
4

1 に答える 1

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 を再度設定していることです (コピー ペースト エラー?)

于 2013-10-10T04:53:18.513 に答える