运行时类型错误,找不到参数



我正在做这个项目,在这个项目中,我在一个可以作为ATM Vestible工作的类中创建方法,但我收到了这个错误:这是我的代码:

class bank_account:
def __init__(self, account_number, name, balance):
self.account_number = account_number
self.name = name
self.balance = balance
def withdraw(self, amount):
if amount > self.balance:
print("Insufficient Funds")
else:
self.balance = self.balance - amount
def deposit(self, amount):
if amount <= 0:
print("Invalid Amount")
else:
self.balance = self.balance + amount
def check_balance(self):
print(self.balance)

account_holder = bank_account(input("Enter your account number, name and balance: "))
transaction = input("Please enter what transaction you would like to do: (Withdraw, Deposit) ")
amount = int(input("Enter the amount you would like to deposit or withdraw: "))
if transaction == 'withdraw' or 'Withdraw':
account_holder.withdraw(amount)
print(account_holder.check_balance())
elif transaction == 'deposit' or 'Deposit':
account_holder.deposit(amount)

account_holder = bank_account(input("Enter your account number, name and balance: "))

TypeError:init((缺少2个必需的位置参数:"name"one_answers"balance">

在创建对象时,在单独的行上输入并传递它们

class bank_account:
def __init__(self, account_number, name, balance):
self.account_number = account_number
self.name = name
self.balance = balance
def withdraw(self, amount):
if amount > self.balance:
print("Insufficient Funds")
else:
self.balance = self.balance - amount
def deposit(self, amount):
if amount <= 0:
print("Invalid Amount")
else:
self.balance = self.balance + amount
def check_balance(self):
print(self.balance)
account_number = int(input("Enter your account number: "))   # <=== Add this
name = input("Enter your Name: ")                            # <=== Add this
balance = int(input("Enter your balance: "))                 # <=== Add this
account_holder = bank_account(account_number, name, balance) # <=== Add this
transaction = input("Please enter what transaction you would like to do: (Withdraw, Deposit) ")
amount = int(input("Enter the amount you would like to deposit or withdraw: "))
if transaction == 'withdraw' or transaction == 'Withdraw':
account_holder.withdraw(amount)
print(account_holder.check_balance())
elif transaction == 'deposit' or transaction == 'Deposit':
account_holder.deposit(amount)

我同意告别者的回答。你也可以只更改一行:

account_holder = bank_account(input("Enter your account number: "), input("Enter your name: "), input("Enter your balance: "))

相关内容

最新更新