这是我当前的粗糙代码:
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
最佳答案
您在课堂上两次定义了self.balance
。
首先是带有余额编号(self.balance = balance
)的类属性(值,浮点),其次是用于打印当前余额(def balance(self):
,将称为self.balance()
)的类方法(函数)。
当您的代码通过调用将class方法和class属性混合在一起时,这会在第72行崩溃(带有上面的错误)。
解决方案使用不同的属性和类名称来解决此问题,例如方法的def get_balance(self):
。
关于python - 'float/str/int'对象不可调用错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/47242986/