[英]object has no attribute outside of class
你好,我正在做一個項目,在創建一個類后,我無法訪問外部的方法以使功能正常工作。 我想使用我班級中的方法來添加或刪除余額和顯示。
任何幫助都會很棒
代碼:
class BankAccount:
def __init__(self, full_name, account_number, routing_number, balance):
self.full_name = full_name
self.account_number = account_number
self.routing_number = routing_number
self.balance = balance
def deposit(amount):
self.balance += amount
print("\n Amount Deposited:", amount)
# Function to withdraw the amount
def withdraw(self):
amount = float((input("Enter a amount")))
if self.balance >= amount:
self.balance -= amount
print("\n You Withdrew", amount)
else:
print("\n Insufficient funds.")
self.balance - 10
# Function to display the amount
def display_balance(self):
print("\n You have ", self.balance)
def add_interest(balance):
interest = balance * 0.00083
def print_receipt():
print(full_name)
print("Account Number: " + account_number)
print("routing number: " + routing_number)
print("balance:" + balance)
# creating an object of class
s = BankAccount("Bob", 00000, 0000, 1)
# Calling functions with that class object
s.withdraw(22)
s.display()
# Calling functions with that class object
您在類方法中缺少對 self 的引用。 嘗試這個:
class BankAccount:
def __init__(self, full_name, account_number, routing_number, balance):
self.full_name = full_name
self.account_number = account_number
self.routing_number = routing_number
self.balance = balance
def deposit(self, amount):
self.balance += amount
print("\n Amount Deposited:", amount)
# Function to withdraw the amount
def withdraw(self):
amount = float((input("Enter a amount")))
if self.balance >= amount:
self.balance -= amount
print("\n You Withdrew", amount)
else:
print("\n Insufficient funds.")
self.balance - 10
# Function to display the amount
def display_balance(self):
print("\n You have ", self.balance)
def add_interest(self, balance):
interest = self.balance * 0.00083
def print_receipt(self):
print(self.full_name)
print("Account Number: " + self.account_number)
print("routing number: " + self.routing_number)
print("balance:" + self.balance)
此外,您正在調用類中未定義的display()
函數。 我相信你的意思是使用display_balance()
。
最后,當您調用withdraw()
函數時,您將輸入一個值作為該函數的參數,但該函數並非設計為接受任何參數。 您編寫它的方式要求用戶在命令行上輸入。 如果你希望它接受參數,你可以像這樣重新定義它:
# Function to withdraw the amount
def withdraw(self, amount):
if self.balance >= amount:
self.balance -= amount
print("\n You Withdrew", amount)
else:
print("\n Insufficient funds.")
self.balance - 10
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.