循环中的if语句,python3

2024-09-30 08:15:45 发布

您现在位置:Python中文网/ 问答频道 /正文

我需要总数来计算…得到0.00

还需要住宅、商业、城市和教区印刷

这是我的最新代码

print("=========================================================")

print(format("Name", '<12s'),format("Type", '<15s'),format("Location",'<10s'),format("KwH", '<8s'),format("Total", '>8s'))

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"):
    custType = "City"
    total = (custKwh * 0.0401) + 6

if (custLoc == "P"):
    custType = "Parish"
    total = (custKwh * 0.04411) + 6.60

print(format(custName, '<12s'),format(custType, '<15s'),format(custLoc, '<10s'),format(custKwh, '<8d'),format(total, '<7.2f'))     

Tags: 代码nameformatinputif商业totalprint
1条回答
网友
1楼 · 发布于 2024-09-30 08:15:45

尝试使用“”

if (custType == "R"):

    custType = Residential

之所以需要将字母加引号,是因为字母本身不加引号,代表一个名为该字母的变量。如果你在它周围加上引号,它的字面意思就是那个字母,或单词。你知道吗

相关问题 更多 >

    热门问题