I'm creating a very basic banking record program in python 3.6. I created a file "accounts.txt" in which users can add their accounts -- name, account number, balance. After they add, the file looks something like this:
213123 |dgfdg |21312.0
123124 |vxcvx |213123.0
123123 |sfsdf |123123.0
column represents account no., name, balance respectively.
I want to delete a particular record from this file. I take account no. as input of the record to be deleted. I just can't figure how to do it. Ex- if the user enters 213123, the entire record should be deleted.
A separate problem in the same case- when I ask the user to input their account number, name, balance, i want to scan "accounts.txt" and if the account number already exists, produce an error instead of creating a new record. Avoid duplicate account numbers.
I'm new to python and this is a basic program so please don't suggest using mysql or mariadb. :D
def account_set():
accno = int(input("Enter Account Number \t"))
name = str(input("Enter Name \t"))
balance = float(input("Enter Balance \t"))
return accno, name, balance
def new():
account = open("accounts.txt", "a+")
info = account_set()
account.seek(0, 2)
for items in info:
account.write("{0:<25} {1}".format(items, "|"))
account.write("\n")
account.close()
def delete():
accno = (input("Enter Account number to delete"))
file = open("accounts.txt", "r+")
print("""
Please choose any one of the Following Options: \n \n
1. New Account \n
2. Delete Account \n
3. Exit
""")
x = True
while x:
user_input = int(input("Enter an option \t"))
if user_input == 1:
new()
elif user_input == 2:
delete()
elif user_input == 3:
print("Thank you for banking with us.")
x = False
else:
print("Invalid Choice")