简体   繁体   English

Python中ATM程序的交易记录

[英]Transaction Record of an ATM Program in Python

My assignment is to keep a record of the transactions of an ATM program in a text file.我的任务是在一个文本文件中记录一个 ATM 程序的交易记录。 After every deposit or withdraw you must update the file with the transaction type, amount deposited/withdrawn, and updated balance.每次存款或取款后,您必须使用交易类型、存款/取款金额和更新的余额更新文件。 I am able to get the text file to print out the current balance but only for one deposit/withdraw.我可以通过文本文件打印出当前余额,但只能用于一次存款/取款。 Additionally, when the program runs it is supposed to open the file with the transactions and find the current balance for the account, but each time I run the program the new text just replaces the old.此外,当程序运行时,它应该打开包含交易的文件并找到帐户的当前余额,但每次运行程序时,新文本只会替换旧文本。 Here is my code:这是我的代码:

balancefile=open("balancefile.txt", "w")
balancefile.write("Starting balance is $10000 \n")
USER_BALANCE=10000
name=input("What is your name? ")
print(name +"," + " " + "Your current balance is: $" + str(USER_BALANCE))
while True:
    answer=input("Would you like to deposit, withdraw, check balance, or exit? ")
    if answer=="deposit" or answer== "Deposit":
        x= input("How much would you like to deposit? ")
        USER_BALANCE += float(x)
        print (name + "," + " " + "Your new balance is: $" + str(USER_BALANCE))
        balancefile.write("You have deposited an amount of $" + x + "." + " " + "Current balance is $" + str(USER_BALANCE) +"\n")
        continue
    elif answer== "withdraw" or answer== "Withdraw":
        y= input("How much would you like to withdraw? ")
        if float (y)<=USER_BALANCE:
            USER_BALANCE -= float(y)
            print (name + "," + " " + "Your new balance is: $" + str(USER_BALANCE))
            balancefile.write("You withdrew an amount of $" + y + "." + " " + "Current balance is $" + str(USER_BALANCE) + "\n")
            continue
        else:
            print ("Cannot be done. You have insufficient funds.")
    elif answer== "check balance" or answer== "Check Balance":
        print ("$" + str(USER_BALANCE))
    elif answer== "exit" or answer== "Exit":
        print ("Goodbye!")
        balancefile.close()
        break
    else:
        print ("I'm sorry, that is not an option")

Please help!请帮忙! To clarify, the original amount in the account is $10,000 but you are supposed to be able to re-enter the program after exiting using the last updated balance in the text file.澄清一下,帐户中的原始金额为 10,000 美元,但您应该能够在退出后使用文本文件中最后更新的余额重新进入程序。

As the commenters have noted , the file mode 'w' will truncate the file proir to write, and 'a' will open a file for appending.正如评论者所指出的,文件模式'w'将截断文件以进行写入,而'a'将打开一个文件进行追加。 This is described in further detail in the Python documentation .在 Python 文档中有更详细的描述。

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

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