1

我正在尝试为学校项目制作一个小型银行软件程序,并且确实为此苦苦挣扎了一段时间。我正在使用python。

现在,这个程序应该做的是能够将名称添加到一个名为 accounts 的文本文件中,然后能够通过输入账户的确切名称来访问这些银行账户,这很好。问题是我只能在文件为空并通过相同的程序运行时添加帐户。

当我尝试停止程序然后重新启动它并向文件中添加更多帐户时,它会要求输入名称和帐户类型,但它应该在第 16 行显示错误,

f.write(accName + "\n")

它显示的错误是

IOError:[Errno 0] 错误

我怎样才能做到这一点,以便用户能够在初始运行和创建文本文件后添加帐户而不删除此行?

searchFile = f.read().splitlines()

这是我的整个程序。

again = "y"
f = open('accounts.txt', 'a+') #Opens the file in a+ mode
searchFile = f.read().splitlines() #Need this to be able to look through the file and write back a certain line, and the next two lines
accessedAccount = []

choice = raw_input("What would you like to do? (add/remove a bank account, access a bank account): ")

if choice == "a":
    while again == "y":
        accName = raw_input("Account owner's name: ")
        accType = raw_input("Account type: ")
        accBal = "0"

        f.write(accName + "\n")
        f.write(accType + "\n")
        f.write(accBal)
        f.write("\n")

        again = raw_input("Add another account?: ")

if choice == "a2":
    account = raw_input("What is the name of the account you wish to access?: ")
    for i, line in enumerate(f):
        if account in line:
            for j in f[i:i+3]:
                print j
                accessedAccount.append(j)

    balance = accessedAccount[2]
    intBalance = int(balance)
    print accessedAccount

    choice2 = raw_input("This is your bank account. What would you like to do now? (Withdraw/deposit, exit): ")
    if choice2 == "d":
        amount = int(raw_input("How much money would you like to deposit? Current balance: %i : " %intBalance))
        newBalance = intBalance + amount
        print "Current balance: %i" %newBalance
        for i, line in enumerate(searchFile):
            if balance in line:
                newBalance = str(newBalance)
                line.replace(balance, newBalance)
                print "test"

f.close
4

0 回答 0