【问题标题】:'float/str/int' object is not callable error'float/str/int' 对象不可调用错误
【发布时间】:2018-04-24 21:00:36
【问题描述】:

这是我目前的粗略代码:

class Bank:
    def __init__(self, name, account_number, balance):
        self.name = name
        self.account_number = account_number
        self.balance = balance
        self.transactions = 0
    def balance(self):
        if self.balance >= 0:
            print("Your balance is $" + str(round(self.balance, 2)) + ".")
        elif self.balance < 0:
            print("Your balance is -$" + str(round(self.balance, 2)) + ".")
    def deposit(self, amount):
        if amount >= 0:
            self.balance = self.balance + amount
            if self.balance >= 0:
                print("You deposited $" + str(round(amount, 2)) + " into your account, your balance is now $" +
                      str(round(self.balance, 2)) + ".")
            elif self.balance < 0:
                print("You deposited $" + str(round(amount, 2)) + " into your account, your balance is now -$" +
                      str(round(self.balance, 2)) + ".")
            self.transactions += 1
        else:
            print("Transaction failed, you cannot deposit a negative amount of money.")
    def withdraw(self, amount):
        if amount >= 0:
            self.balance = self.balance - amount
            if self.balance >= 0:
                print("You withdrew $" + str(round(amount, 2)) + " out of your account, your balance is now $" +
                      str(round(self.balance, 2)) + ".")
            elif self.balance < 0:
                print("You withdrew $" + str(round(amount, 2)) + " out of your account, your balance is now -$" +
                      str(round(self.balance, 2)) + ".")
            self.transactions += 1
        else:
            print("Transaction failed, you cannot withdraw a negative amount of money.")
    def summary(self):
        if self.balance >= 0:
            print("Hi " + self.name + " with account# " + str(self.account_number) + ", you have made " +
                  str(self.transactions) + " transactions and your current balance is now $" +
                  str(round(self.balance, 2)) + ".")
        elif self.balance < 0:
            print("Hi " + self.name + " with account# " + str(self.account_number) + ", you have made " +
                  str(self.transactions) + " transactions and your current balance is now -$" +
                  str(round(self.balance, 2)) + ".")

name1 = str(input("Please enter your name: "))
account_number1 = int(input("Please enter " + name1 + "'s account#: "))
balance1 = float(input("Please enter " + name1 + "'s account balance: "))

name2 = str(input("Please enter your name: "))
account_number2 = int(input("Please enter " + name2 + "'s account#: "))
balance2 = float(input("Please enter " + name2 + "'s account balance: "))

name3 = str(input("Please enter your name: "))
account_number3 = int(input("Please enter " + name3 + "'s account#: "))
balance3 = float(input("Please enter " + name3 + "'s account balance: "))

object1 = Bank(name1, account_number1, balance1)
object2 = Bank(name2, account_number2, balance2)
object3 = Bank(name3, account_number3, balance3)

end = False
while end == False:
x = int(input("Enter 1 to use " + name1 + "'s bank account, enter 2 to use " +
              name2 + "'s bank account, enter 3 to use " + name3 + "'s bank account, or enter 4"
                                                                   "to end all transactions (quit): "))
if x == 1:
    a = int(input("Enter 1 to show " + name1 + "'s account balance, enter 2 to deposit money into " +
                  name1 + "'s account, enter 3 to withdraw money from " + name1 +
                  "'s account, or enter 4 to go back and choose another bank account: "))
    if a == 1:
        object1.balance()
    elif a == 2:
        amount = float(input("Please enter the amount of money you wish to deposit: "))
        object1.deposit(amount)
    elif a == 3:
        amount = float(input("Please enter the amount of money you wish to withdraw: "))
        object1.withdraw(amount)
    elif a == 4:
        continue
    else:
        print("Invalid number, restarting.")
        continue
if x == 2:
    a = int(input("Enter 1 to show " + name2 + "'s account balance, enter 2 to deposit money into " +
                  name2 + "'s account, enter 3 to withdraw money from " + name2 +
                  "'s account, or enter 4 to go back and choose another bank account: "))
    if a == 1:
        object2.balance()
    elif a == 2:
        amount = float(input("Please enter the amount of money you wish to deposit: "))
        object2.deposit(amount)
    elif a == 3:
        amount = float(input("Please enter the amount of money you wish to withdraw: "))
        object2.withdraw(amount)
    elif a == 4:
        continue
    else:
        print("Invalid number, restarting.")
        continue
if x == 3:
    a = int(input("Enter 1 to show " + name3 + "'s account balance, enter 2 to deposit money into " +
                  name3 + "'s account, enter 3 to withdraw money from " + name3 +
                  "'s account, or enter 4 to go back and choose another bank account: "))
    if a == 1:
        object3.balance()
    elif a == 2:
        amount = float(input("Please enter the amount of money you wish to deposit: "))
        object3.deposit(amount)
    elif a == 3:
        amount = float(input("Please enter the amount of money you wish to withdraw: "))
        object3.withdraw(amount)
    elif a == 4:
        continue
    else:
        print("Invalid number, restarting.")
        continue
if x == 4:
    object1.summary()
    object2.summary()
    object3.summary()
    end = True

由于某种原因,当我尝试在课堂上使用 balance 函数时,我不断收到此错误:

Traceback (most recent call last):

  File "Larger Class Assignment.py", line 72, in <module>

   object1.balance()

TypeError: 'float' object is not callable

这正是我在代码中输入的内容:

  • 请输入您的姓名:Errin
  • 请输入 Errin 的帐号#: 234623465
  • 请输入 Errin 的账户余额:500
  • 请输入您的姓名:asfdgafd
  • 请输入asfdgafd的账号#: 6256
  • 请输入asfdgafd的账户余额:4362
  • 请输入您的姓名:gsfgh
  • 请输入gsfgh的账号#: 3546
  • 请输入gsfgh的账户余额:3456
  • 输入 1 使用 Errin 的银行帐户,输入 2 使用 asfdgafd 的银行 账户,输入3使用gsfgh的银行账户,或输入4结束所有 交易(退出):1
  • 输入 1 显示 Errin 的账户余额,输入 2 存款 进入 Errin 的账户,输入 3 从 Errin 的账户中提款, 或输入 4 返回并选择另一个银行帐户:1

【问题讨论】:

    标签: python python-3.x class object


    【解决方案1】:

    您在课堂上定义了两次self.balance

    首先作为类属性(value,float)与余额数量(self.balance = balance),其次作为类方法(函数)打印您当前的余额(def balance(self): 将被称为self.balance())。

    当您的代码通过调用它混淆了类方法和类属性时,这会在第 72 行崩溃(出现上述错误)。

    解决方案 使用不同的属性和类名来解决这个问题,例如def get_balance(self): 方法。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-16
      • 1970-01-01
      相关资源
      最近更新 更多