Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python中ATM程序的事务记录_Python_While Loop_Transactions_Text Files_Updating - Fatal编程技术网

Python中ATM程序的事务记录

Python中ATM程序的事务记录,python,while-loop,transactions,text-files,updating,Python,While Loop,Transactions,Text Files,Updating,我的任务是将ATM程序的交易记录保存在文本文件中。每次存款或取款后,您必须使用交易类型、存款/取款金额和更新的余额更新文件。我可以让文本文件打印当前余额,但只能打印一次存款/取款。此外,当程序运行时,它应该打开包含交易的文件并查找帐户的当前余额,但每次我运行程序时,新文本只会替换旧文本。 这是我的密码: balancefile=open("balancefile.txt", "w") balancefile.write("Starting balance is $10000 \n") USER_

我的任务是将ATM程序的交易记录保存在文本文件中。每次存款或取款后,您必须使用交易类型、存款/取款金额和更新的余额更新文件。我可以让文本文件打印当前余额,但只能打印一次存款/取款。此外,当程序运行时,它应该打开包含交易的文件并查找帐户的当前余额,但每次我运行程序时,新文本只会替换旧文本。 这是我的密码:

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")
balancefile=open(“balancefile.txt”、“w”)
balancefile.write(“起始余额为$10000\n”)
用户余额=10000
name=输入(“你叫什么名字?”)
打印(名称+”,“+”+”您当前的余额为:$“+str(用户余额))
尽管如此:
回答=输入(“您想存款、取款、检查余额还是退出?”)
如果答案=“存款”或答案=“存款”:
x=输入(“您想存多少?”)
用户\余额+=浮动(x)
打印(名称+”,“+”+”您的新余额为:$“+str(用户余额))
balancefile.write(“您已存入$”+x+“+”+”当前余额为$”+str(用户余额)+“\n”)
持续
elif answer==“撤回”或answer==“撤回”:
y=输入(“您想提取多少?”)

如果float(y)As,则文件模式
'w'
将截断要写入的文件,而
'a'
将打开一个文件进行追加。这将被更详细地描述

尝试使用“a”标志打开。您需要打开文件以附加
balancefile=open(“balancefile.txt”,“a”)
您还在脚本开始时将余额设置为10000,而不管文件上有多少余额。您应该首先进行检查-如果没有balancefile.txt,或者balancefile.txt中还没有记录任何余额,则将余额设置为10000。否则,读取文件中最后记录的余额,并将余额设置为该值。