开发者社区> 问答> 正文

Python帮助-具有“分配前引用的局部变量”

我在外壳上说在赋值之前引用了局部变量时遇到了问题,并且觉得之前的答案都没有帮助。我可以对此代码提出一些具体建议吗?

错误:TotalExcessCharge = ExcessOneCharge + ExcessTwoCharge + ExcessThreeCharge + ExcessFourCharge + ExcessFiveCharge + ExcessPlusLimitCharge UnboundLocalError:局部变量'ExcessThreeCharge'在分配前被引用

def BillingSystem(CustomerName,CustomerType,TotalGBUsed):
    StandardCustomer = 1500
    StandardQuota = 25
    PremiumCustomer = 2500
    PremiumQuota = 50

    if (CustomerType == "Standard") or (CustomerType == "standard"):
        if (TotalGBUsed > StandardQuota):
            ExcessGB = TotalGBUsed - StandardQuota
            for a in range(0, ExcessGB):
                if (a <= 10):
                    ExcessOne = 250
                    ExcessOneCharge = a * ExcessOne

            for b in range(0, ExcessGB):
                if (b > 10) and (b <= 20):
                    ExcessTwo = 500
                    ExcessTwoCharge = b * ExcessTwo

            for c in range(0, ExcessGB):
                if (c > 20) and (c <= 30):
                    ExcessThree = 750
                    ExcessThreeCharge = c * ExcessThree

            for d in range(0, ExcessGB):
                if (d > 30) and (d <= 40):
                    ExcessFour = 1000
                    ExcessFourCharge = d * ExcessFour

            for e in range(0, ExcessGB):
                if (e > 40) and (e <= 50):
                    ExcessFive = 1250
                    ExcessFiveCharge = e * ExcessFive

            for explus in range(0, ExcessGB):
                if (explus > 50):
                    ExcessPlusLimit = 1500
                    ExcessPlusLimitCharge = explus * ExcessPlusLimit

        TotalExcessCharge = ExcessOneCharge + ExcessTwoCharge + ExcessThreeCharge + ExcessFourCharge + ExcessFiveCharge + ExcessPlusLimitCharge
        TotalCharge = StandardCustomer + TotalExcessCharge

        print ("Total Excess Charge : " + str(TotalExcessCharge))
        print ("Total Charge for this month : " + str(TotalCharge))

    else:
        print ("Total Excess Charge : 0")
        print ("Total Charge for this month : " + str(StandardCustomer))

CName = input("[!] Customer Name : ")
CType = input("[!] Customer Type : ")
TotGB = int(input("[!] Total GB Usage : "))
BillingSystem(CName,CType,TotGB)

问题来源:stackoverflow

展开
收起
is大龙 2020-03-24 21:37:30 647 0
1 条回答
写回答
取消 提交回答
  • 这里的问题是,当您的代码没有进入if条件时,您的变量永远不会被初始化,但是您在最后引用了它们。已分配。始终确保引用分配的变量!

    而且您还可以使代码更易于阅读,例如

    • 直接在if条件内使用多余的#值,而不将其分配给变量。
    • 对输入字符串使用 转化为大小写一致后 一次性比较。。

      def BillingSystem(CustomerName,CustomerType,TotalGBUsed): StandardCustomer = 1500 StandardQuota = 25 PremiumCustomer = 2500 PremiumQuota = 50

      ExcessOneCharge=0
      ExcessTwoCharge=0
      ExcessThreeCharge=0
      ExcessFourCharge=0
      ExcessFiveCharge=0
      ExcessPlusLimitCharge=0
      
      
      if (CustomerType.upper() == "STANDARD"):
          if (TotalGBUsed > StandardQuota):
              ExcessGB = TotalGBUsed - StandardQuota
              for a in range(0, ExcessGB):
                  if (a <= 10):
                      ExcessOneCharge = a * 250
                  elif (a > 10) and (a <= 20):
                      ExcessTwoCharge = (a - 10) * 500
                  elif (a > 20) and (a <= 30):
                      ExcessThreeCharge = (a - 20) * 750
                  elif (a > 30) and (a <= 40):
                      ExcessFourCharge = (a - 30) * 1000
                  elif (a > 40) and (a <= 50):         
                      ExcessFiveCharge = (a - 40) * 1250
                  elif (a > 50):
                      ExcessPlusLimitCharge = (a - 50) * 1500
      
          TotalExcessCharge = ExcessOneCharge + 
                              ExcessTwoCharge + 
                              ExcessThreeCharge + 
                              ExcessFourCharge + 
                              ExcessFiveCharge + 
                              ExcessPlusLimitCharge
      
          TotalCharge = StandardCustomer + TotalExcessCharge
      
          print ("Total Excess Charge : ", TotalExcessCharge)
          print ("Total Charge for this month : ", TotalCharge)
      
      else:
          print ("Total Excess Charge : 0")
          print ("Total Charge for this month : ", StandardCustomer)
      

      CName = input("[!] Customer Name : ") CType = input("[!] Customer Type : ") TotGB = int(input("[!] Total GB Usage : ")) BillingSystem(CName,CType,TotGB)

    而且,除了创建ExcessOneCharge,ExcessTwoCharge变量等之外,您还可以执行以下操作:

    TotalExcessCharge = 0 #don't forget to initiate the variable at the beginning of the function
    #then inside the if conditions
    TotalExcessCharge += a\*xcess#
    

    这只是如何编写更简洁的代码的示例...您可以根据需要应用逻辑!

    注意:我在手机上输入所有内容,因此请忽略错别字...

    回答来源:stackoverflow

    2020-03-24 21:37:38
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
From Python Scikit-Learn to Sc 立即下载
Data Pre-Processing in Python: 立即下载
双剑合璧-Python和大数据计算平台的结合 立即下载