2017-10-25 49 views
-1

私の割り当ては、ATMプログラムのトランザクションの記録をテキストファイルに保存することです。すべての入金または払い戻しの後、取引タイプ、入金/引落金額、更新残高をファイルに更新する必要があります。私は現在の残高を印刷するためにテキストファイルを取得することができますが、1つの預金/払い戻しに対してのみ使用できます。さらに、プログラムが実行されると、トランザクションでファイルを開き、アカウントの現在の残高を見つけることになっていますが、プログラムを実行するたびに新しいテキストが古いものに置き換えられます。 私のコードは次のとおりです。Pythonでの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") 

助けてください!明確にするために、アカウントの元の金額は10,000ドルですが、テキストファイルの最後に更新された残高を使用して終了した後にプログラムを再入力できるはずです。

+0

'a'フラグでオープンしてみてください。 – ergonaut

+0

'balancefile = open(" balancefile.txt "、" a ")を追加するためにファイルを開く必要があります – 0TTT0

+0

ファイルの残高に関係なく、スクリプトの開始時に残高を10,000に設定しています。最初にチェックしてください。balancefile.txtがない場合、またはbalancefile.txtに残高がまだ記録されていない場合は、残高を10,000に設定してください。それ以外の場合は、ファイル内の最後に記録された残高を読み取り、残高をその値に設定します。 –

答えて

関連する問題