带循环的Python类:银行账户

时间:2013-05-05 19:37:11

标签: python class loops input

我遇到了一个很大的问题而且我真的不知道我写的代码有什么问题,它肯定与我为输入文件设置的输入有关但我在墙上而且我真的不知道如何继续

我输入的名称错误,但这就是本书向我展示如何设置第一个存款的方式。输入文件位于主程序之下。

我的问题是,是否有人知道我在主要内容中提出的输入有什么问题?它们似乎是正确的,但我一直得到一个NameError:ABC123没有为我试图推出的每一个更改定义。

class BankAccount:
    def __init__(self, getbankaccount, inputAmount=0):
        self.__balance = inputAmount
        self.__numDeposits = 0
        self.__numWithdrawals = 0
        self.__totalDeposits = 0
        self.__totalWithdrawals = 0
        self.__getbankaccount=getbankaccount

    def getBalance(self):
        return self.__balance

    def getNumDeposits(self):
        return self.__numDeposits

    def getNumWithdrawals(self):
        return self.__numWithdrawals

    def getTotalDeposits(self):
        return self.__totalDeposits

    def getTotalWithdrawals(self):
        return self.__totalWithdrawals

    def getbankaccount(self):
        return self.__getbankaccount

    def Deposit(self,amount):
        self.__balance = self.__balance + amount
        self.__numDeposits = self.__numDeposits + 1
        self.__totalDeposits = self.__totalDeposits + amount
        return self.__balance

    def Withdrawal(self,amount):
        if (self.__balance >= amount):
            self.__balance = self.__balance - amount
            self.__numWithdrawals = self.__numWithdrawals + 1
            self.__totalWithdrawals = self.__totalWithdrawals + amount
            return True
        else:
            return False

def main():
    acctlist=[]
    account=input()
    amount=eval(input())
    type=input()

    while(type!='#'):
        if (type=='D'):
            account=input()
            amount=eval(input())
            for i in range(len(acctlist())):
                if (account==acctlist[i].getbankaccount()):
                    index=i
                acctlist[index].Deposit(amount)
                PrintSuccessfullTrans(account, type, amount, acctlist[index].getBalance())
        elif (type=='W'):
            account=input()
            amount=eval(input())
            for i in range(len(acctlist())):
                if (account==acctlist[i].getbankaccount()):
                    index=i
                    status=(acctlist[index].Withdrawal(amount))
                    if (status == True):
                        PrintSuccessfulTrans(account, type, amount, acctlist[index].getbalance())
                    else:
                        PrintDenied(account, type, amount, "<DENIED>")
        elif (type == 'B'):
            type=ConvertType(type)
            amount=eval(input)
            print(acctlist[i].getbankaccount(), amount)
        elif (type == 'T'):
            account1=input()
            account2=input()
            amount=eval(input())
            for i in range(len(acctlist())):
                if (accoun1t==acctlist[i].getbankaccount()):
                    index=i
                    acctlist[index].Deposit(amount)
                    if status == True:
                        print('Test1')
                    else:
                        print('Test2')
            for g in range(len(acctlist())):
                if (account2==acctlist[i].getbankaccount()):
                    index=g
                    acctlist[index].Withdrawal(amount)

        account=input()
        amount=eval(input())
        type=input()


def PrintSuccessfulTrans(account,type, amount, balance):
                typeName = ConvertType(type)
                print("{:8s}{:12}{:8.2f}{:12.2f}".format(account,typeName,amount,balance))


def PrintDenied(account,type,amount, balance):
                typeName = ConvertType(type)
                print("{:8s}{:12}{:8.2f}{:>12}".format(account,typeName,amount,balance))

def ConvertType(type):
                if (type == 'D'):
                    return "Deposit"

                elif (type == 'W'):
                    return "Withdrawal"

                elif (type == 'B'):
                    return "Balance"
main()
11
ABC123
500
DEF456
1000
XYZ999
2056
MNI612
2012
CKE909
1313
THM115
5783
WBS319
2041
GFL522
10238
JDT121
876
JFL216
9218
GRP904
820
W
JFL216
587.24
D
MNI612
420.45
D
XYZ999
214.38
W
THM115
1045.23
D
WBS319
258.30
W
JDT121
924.10
T
GFL522
JDT121
2000
B
GFL522
0
B
XYZ999
0
T
DEF456
ABC123
50.50
#
#

1 个答案:

答案 0 :(得分:0)

当面对这样的事情时,尝试以交互方式调用python并尝试一下:

$ python
Python 2.7.3 (default, Mar 12 2013, 16:26:07) 
[GCC 4.2.1 20070831 patched [FreeBSD]] on freebsd9
Type "help", "copyright", "credits" or "license" for more information.
>>> input()
11
11
>>> input()
ABC123
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<string>", line 1, in <module>
NameError: name 'ABC123' is not defined

你去了,第一个问题被复制了。

大概你想要像raw_input()(或python 3.x,这就是input现在所做的那样)。

在没有评论其他所有内容的情况下,我会在此注意到你的调度循环还会检查类型'D','W','B'和'T',但不是'11','ABC123',等等。据推测,您希望首先设置帐户列表,然后才处理字母编码的事务。