执行此操作时,我收到错误消息:
D:\SARFARAZ\Python>Python assignment_01.py
Traceback (most recent call last):
File "assignment_01.py", line 35, in <module>
BankAcc1 = BankAcc('Sarfaraz',12345,50000)
TypeError: object() takes no parameters
代码:
#Assignment_01 : Class Creation
class BankAcc(object):
int_rate = 0.7
balance = 0
#def **_init_**(self, name, number, balance): --> error was in this line, I've corrected it now as below
def __init__(self, name, number, balance):
self.name = name
self.number = number
self.balance = balance
def withdraw(self, amount):
self.balance -= amount
return self.balance
def deposit(self, amount):
self.balance += amount
return self.balance
def add_interest(self):
self.interest = int_rate * self.balance
self.balance += self.interest
return self.balance
'''class MinimumBalanceAccount(BankAccount):
def __init__(self, min_bal):
BankAccount.__init__(self)
self.min_bal = 500
def withdraw(self, amount):
if self.balance - amount < self.min_bal:
print ("Sorry, minimum balance must be maintained.")
else:
BankAccount.withdraw(self, amount)'''
BankAcc1 = BankAcc('Sarfaraz',12345,50000)
BankAcc2 = BankAcc1.withdraw(1000)
print (BankAcc2)
我正在尝试创建一个对象,然后尝试调用 withdraw()方法 在取消一些金额后打印余额。
答案 0 :(得分:2)
您的__init__
方法名称中有拼写错误:
def _init_(self, name, number, balance):
您需要在开头和结尾使用两个下划线。
如果没有有效命名的__init__
方法,Python将回退到object.__init__
,它不带任何参数。