蟒蛇。无法运行程序

时间:2017-01-29 10:03:04

标签: python

我是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

1 个答案:

答案 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)