제 할당은 ATM 프로그램의 트랜잭션 레코드를 텍스트 파일에 보관하는 것입니다. 모든 입금 또는 인출 후에는 거래 유형, 입금액/출금액 및 갱신 된 잔액으로 파일을 갱신해야합니다. 현재 잔액을 출력하는 텍스트 파일을 가져올 수 있지만 하나의 입금/인출에만 사용할 수 있습니다. 또한 프로그램이 실행되면 트랜잭션을 사용하여 파일을 열고 계정의 현재 잔액을 찾지 만 프로그램을 실행할 때마다 새 텍스트가 이전 데이터를 대체합니다. 내 코드는 다음과 같습니다.파이썬에서 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 달러이지만 텍스트 파일의 마지막 업데이트 잔액을 사용하여 종료 한 후 프로그램에 다시 입력 할 수 있어야합니다.
'a'플래그로 열어 봅니다. – ergonaut
'balancefile = open ("balancefile.txt", "a")로 덧붙이 기 위해 파일을 열어야합니다. – 0TTT0
파일의 어떤 잔고에 관계없이 스크립트 시작시 잔액을 10,000으로 설정합니다. 먼저 balancefile.txt가 없거나 balancefile.txt에 아직 잔액이 기록되어 있지 않으면 잔액을 10,000으로 설정하십시오. 그렇지 않으면 파일에 마지막으로 기록 된 잔액을 읽고 잔액을 해당 값으로 설정하십시오. –