簡體   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