繁体   English   中英

Python3 if循环语句

[英]Python3 if Statement in a Loop

我的输出应为“住宅,企业,城市和教区”时,得到的输出为“ 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

我将其重写为:

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(复制粘贴错误?)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM