繁体   English   中英

Python。 无法运行程序

[英]python. Unable to run program

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

你的问题是缩进。 将您的代码逻辑移动到该行的开头将执行您的代码。

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)

输出:

Account c123
Account count = 1

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM