这是我目前的粗略代码:
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
这正是我在代码中输入的内容:
答案 0 :(得分:2)
您在班级中定义了两次self.balance
。
首先作为类属性(值,浮点数)和余额(self.balance = balance
),其次作为类方法(函数)来打印你的当前余额(def balance(self):
,将被称为self.balance()
)。
当你的代码通过调用它来混合类方法和类属性时,这会在第72行崩溃(上面的错误)。
解决方案使用不同的属性和类名来解决此问题,例如该方法def get_balance(self):
。