【问题标题】:Python code for class Not returning value类的Python代码不返回值
【发布时间】:2017-07-30 15:35:37
【问题描述】:

你好,我有一个 python 代码不断给我一个错误,在我继续之前有人可以看看它。这是为了让我的 python 类计算手机账单金额。我现在有这个但它没有返回值

经济计划包括 30 美元的无限语音和文本、10 美元的 1 GB 数据和每 GB 6 美元。

正常计划包括 30 美元的无限制语音和文本、5 GB 数据(30 美元)和每 GB 4 美元。

class cellphone:
    cost = 30
    due = 0
    amountGB = 0
    Total = 0

    def econPlan (self, gB, gbOver):
        self.cost = 30
        self.gB = 1
        self.gbOver = 6
        self.due = 0

        while amountGB >= self.gB:
            int(amountGB - self.gB)* self.gbOver
        return self.due

        while cPlan == Econ and amountGB >= self.gB:
            print ("$",self.due)

    def normPlan (self, gB, gbOver):
        self.cost = 30
        self.gB = 5
        self.gbOver = 4

        while amountGB >= self.gB:
            int(amountGB - self.gB)* self.gbOver
        return self.due

        while cPlan == Norm and amountGB >= self.gB:
            print ("$",self.due)






print (" Which Plan do you have: econ or norm?")
print (" Econ is $30 unlimited voice and text, 1 GB data for $10, and $6 for every GB over.")
print (" Norm is $30 unlimited voice and text, 5 GB data for $30, and $4 for every GB over.")

econ = cellphone.econPlan
norm = cellphone.normPlan
cellphone.cPlan = input ("So which plan do you have? >>")
if cellphone.cPlan == econ:
    print ("you have the Economy plan")

elif cellphone.cPlan == norm:
    print ("you have the Normal plan")

cellphone.amountGB = input ("how many GB did you used >>")
if cellphone.cPlan == "Econ":
    print (cellphone.due)

【问题讨论】:

  • 发布错误...
  • 错误是什么?
  • self在哪里?
  • 是自己需要的
  • 如果类方法中的块没有适当的缩进。

标签: python python-3.x


【解决方案1】:

Self 只存在于方法中。如果要在方法中获取变量的当前值,请调用该方法并返回该变量。

【讨论】:

    猜你喜欢
    • 2021-03-31
    • 1970-01-01
    • 1970-01-01
    • 2015-07-23
    • 2012-09-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-04
    相关资源
    最近更新 更多