gpt4 book ai didi

python - __init__() 采用 1 个位置参数,但给出了 2 个

转载 作者:行者123 更新时间:2023-11-28 22:33:05 24 4
gpt4 key购买 nike

我已经通读了有关此错误的其他帖子,我认为我已经解决了问题,但我仍然遇到问题。

我已经在适当的空间包含了必要的 self 参数,但我仍然收到错误:

Traceback (most recent call last):
File "...", line 30, in <module>
JohnSmith = CheckingAccount(20000)
File "...", line 18, in __init__
BankAccount.__init__(self, initBal)
TypeError: __init__() takes 1 positional argument but 2 were given

class BankAccount (object):
# define class for bank account
def __init__ (self):
# initialize bank account w/ balance of zero
self.balance = 0
def deposit (self, amount):
# deposit the given amount into account
self.balance = self.balance + amount
def withdraw (self, amount):
# withdraw the given amount from account
self.balance = self.balance - amount
def getBalance (self):
# return account balance
return self.balance

class CheckingAccount (BankAccount):
def __init__ (self, initBal):
BankAccount.__init__(self, initBal)
self.checkRecord = {}
def processCheck (self, number, toWho, amount):
self.withdraw(amount)
self.checkRecord[number] = (toWho, amount)
def checkInfo (self, number):
if self.checkRecord.has_key(number):
return self.checkRecord [ number ]
else:
return 'No Such Check'

# create checking account
JohnSmith = CheckingAccount(20000)
JohnSmith.processCheck(19371554951,'US Bank - Mortgage', 1200)
print (JohnSmith.checkInfo(19371554951))
JohnSmith.deposit(1000)
JohnSmith.withdraw(4000)
JohnSmith.withdraw(3500)

最佳答案

您可能想将 BankAccount 重新定义为

class BankAccount(object):
def __init__(self, init_bal=0):
self.balance = init_bal

# ...

关于python - __init__() 采用 1 个位置参数,但给出了 2 个,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40312491/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com