我不確定我做錯了什么。我多次瀏覽代碼,但無法弄清楚為什么它不起作用。我想當你創建一個類時,你不必在創建它的實體時“定義”它?
這是代碼:
class BankAccount:
#init method with 3 parameters(self, balance & name)
def __init__(self, balance, name):
#variables and their assignments
self.balance = balance
self.name = name
num_deposits = 0
deposit_total = 0
num_withdraws = 0
withdraw_total = 0
#numDeposit method with 1 parameter, itself.
def numDeposits(self):
#refer to num_deposits instance and increment by 1
num_deposit = 1
#'numWithdraw' method
def numWithdraws(self):
#update instance
num_withdraws = 1
#method named 'Deposit" that takes in 2 parameters(self & amount)
def Deposit(self, amount):
#update balance instance by (amount)
balance = amount
#update deposit_total instance by amount
deposit_total = amount
#calling numDeposit method to update number of total deposits
numDeposits()
#method named 'withdraw' that takes in 2 parameters(self & amount)
def withdraw (self, amount):
#updating instance variables 'balance' and 'withdraw_total'
balance -= 1
withdraw_total = 1
#calling 'numWithdraws' method
numWithdraws()
# 'endOfMonth' method that has 1 parameter (self)
def endOfMonth(self):
#print statements
print("Bank account: ", name)
print("Balance : ", balance)
print("Number of deposits: ", num_deposit, "totalling ", deposit_total)
print("Number of withdraws: ", num_withdraws, "totalling ", withdraw_total)
# 2 instances of BankAccount
BankAccount1 = BankAccount( 0 , "chase" )
BankAccount2 = BankAccount( 100, "Bank of America")
#invoking deposit and withdraw for instance 1
BankAccount1.deposit(50)
BankAccount1.deposit(50)
BankAccount1.withdraw(100)
BankAccount1.withdraw(100)
BankAccount1.withdraw(100)
#calling endOfMonth method for instance 1
BankAccount1.endOfMonth()
#invoking deposit and withdraw for instance 2
BankAccount2.deposit(25)
BankAccount2.deposit(25)
BankAcconut2.deposit(5000)
BankAccount2.withdraw(10)
BankAccount2.withdraw(1000)
BankAccount2.withdraw(70)
#calling endOfMOnth method for instance 2
BankAccount2.endOfMonth()
每次我運行代碼時,我都會收到一個 NameError,它說沒有定義名稱“BankAccount”?我想要的結果是:
銀行賬戶:Chase 余額:$-200 存款次數:2 次總計 100 美元取款次數:3 次總計 300 美元
以此類推為銀行賬戶的第二個實體
uj5u.com熱心網友回復:
您在縮進和語法方面存在一些問題。
使用類屬性時,如果要更改該類實體的屬性或使用方法,則需要使用self.前綴:
def numDeposits(self):
num_deposits = 1 # this just alters an undefined variable called num_deposits
self.num_deposits = 1 # this is what you need
其次,您的屬性需要在任何方法的范圍之外進行定義,以按照您希望的方式作業。
還有一些其他的東西你可以改進,比如檔案字串是描述性的但不是多余的,或者使用它們沒有意義(帶有 3 個引數的 init 方法是不言自明的)。
class BankAccount:
num_deposits = 0
deposit_total = 0
num_withdraws = 0
withdraw_total = 0
#init method with 3 parameters(self, balance & name)
def __init__(self, balance, name):
#variables and their assignments
self.balance = balance
self.name = name
#numDeposit method with 1 parameter, itself.
def numDeposits(self):
#refer to num_deposits instance and increment by 1
self.num_deposits = 1
#'numWithdraw' method
def numWithdraws(self):
#update instance
self.num_withdraws = 1
#method named 'Deposit" that takes in 2 parameters(self & amount)
def deposit(self, amount):
#update balance instance by (amount)
self.balance = amount
#update deposit_total instance by amount
self.deposit_total = amount
#calling numDeposit method to update number of total deposits
self.numDeposits()
#method named 'withdraw' that takes in 2 parameters(self & amount)
def withdraw (self, amount):
#updating instance variables 'balance' and 'withdraw_total'
self.balance -= 1
self.withdraw_total = 1
#calling 'numWithdraws' method
self.numWithdraws()
# 'endOfMonth' method that has 1 parameter (self)
def endOfMonth(self):
#print statements
print("Bank account: ", self.name)
print("Balance : ", self.balance)
print("Number of deposits: ", self.num_deposits, "totalling ", self.deposit_total)
print("Number of withdraws: ", self.num_withdraws, "totalling ", self.withdraw_total)
# 2 instances of BankAccount
BankAccount1 = BankAccount( 0 , "chase" )
BankAccount2 = BankAccount( 100, "Bank of America")
#invoking deposit and withdraw for instance 1
BankAccount1.deposit(50)
BankAccount1.deposit(50)
BankAccount1.withdraw(100)
BankAccount1.withdraw(100)
BankAccount1.withdraw(100)
#calling endOfMonth method for instance 1
BankAccount1.endOfMonth()
#invoking deposit and withdraw for instance 2
BankAccount2.deposit(25)
BankAccount2.deposit(25)
BankAccount2.deposit(5000)
BankAccount2.withdraw(10)
BankAccount2.withdraw(1000)
BankAccount2.withdraw(70)
#calling endOfMOnth method for instance 2
BankAccount2.endOfMonth()
uj5u.com熱心網友回復:
您創建的 bankAccount 實體仍然在類中縮進。這使得 bankAccount 呼叫在類創建中完成。由于該類在創建程序中尚不存在,因此會引發 NameError。因此,您應該取消縮進它們。如果你真的想將它們設定為類屬性,你應該使用bankAccount.BankAccount1=...unindented。
uj5u.com熱心網友回復:
根據縮進,您似乎正在創建 BankAccount1 和 2 并在類 BankAccount 的定義中使用它們?在該命名空間中,BankAccount 不存在。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/338094.html
上一篇:新班級給出不正確的回報
