0

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

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

我的问题是,有谁知道我主要列出的输入有什么问题?它们似乎是正确的,但我不断收到 NameError: ABC123 is not defined for each change I try out。

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
#
#
4

1 回答 1

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”等. 大概您想先设置帐户列表,然后再处理字母编码的交易。

于 2013-05-05T19:52:11.160 回答