【问题标题】:if statement in a loop, python3 [closed]if 循环中的语句,python3 [关闭]
【发布时间】:2013-10-17 15:27:46
【问题描述】:

我需要计算总数...得到 0.00

还需要 Residential、Business、City 和 Parish 才能打印

这是我更新的代码,

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'))     

【问题讨论】:

  • 我从数据文件中获取输入,输入是 R、B、C 或 P

标签: python loops


【解决方案1】:

尝试使用“”

if (custType == "R"):

    custType = Residential

您需要将字母放在引号中的原因是因为它自己的字母,没有引号,代表一个名称为该字母的变量。如果你在它周围加上引号,它的字面意思就是那个字母或单词。

【讨论】:

  • 甜蜜,这似乎有工作,现在我得到错误:'total' is not defined...我如何定义'total'所以它计算KwH的总成本取决于它的城市、教区或企业
  • 因为我无法在其中编辑它... 您需要将字母放在引号中的原因是因为字母本身,没有引号,代表一个名称为信件。如果你在它周围加上引号,它的字面意思就是那个字母或单词。
  • 像这样在开头定义总计= 0.0
  • 所以我一开始就设置了total = 0,现在总打印为0.00,它不计算。我得到的字母是“R、B、C 和 P”,而不是“住宅、商业、城市和教区”
  • custType = "Resisdential"
猜你喜欢
  • 1970-01-01
  • 2016-12-27
  • 1970-01-01
  • 2013-01-27
  • 1970-01-01
  • 1970-01-01
  • 2013-04-20
  • 1970-01-01
  • 2016-06-22
相关资源
最近更新 更多