我是Python编程的新手。 我试图实现以下输出:
Account c
Account count = 1
Successful transaction! Balance = 10000
Successful transaction! Balance = 9000
Not enough balance
我的代码:
class Account:
accountCount = 0
def __init__(self, name, accountNo):
self.name = name
self.accountNo = accountNo
self.balance = 0
Account.accountCount += 1
print ("Account " + self.accountNo)
print ("Account count= " +str(Account.accountCount))
def withdraw (self, amount):
self.balance -= amount
return self.balance
def deposit (self,amount):
self.balance += amount
return self.balance
myAccount = Account ("c", "c123")
myAccount.deposit(10000)
myAccount.withdraw(500)
myAccount.withdraw(10000)
我收到以下错误
line 1, in <module>
line 20, in Account myAccount = Account ("c", "c123")
NameError: name 'Account' is not defined
答案 0 :(得分:0)
你的问题是缩进。将代码逻辑移动到行的开头将执行代码。
float
输出:
class Account:
accountCount = 0
def __init__(self, name, accountNo):
self.name = name
self.accountNo = accountNo
self.balance = 0
Account.accountCount += 1
print("Account " + self.accountNo)
print("Account count = " + str(Account.accountCount))
def withdraw(self, amount):
self.balance -= amount
return self.balance
def deposit(self, amount):
self.balance += amount
return self.balance
account_c = Account("c", "c123")
account_c.deposit(10000)
account_c.withdraw(500)
account_c.withdraw(10000)