I would like to expand my account management by allowing customers to open a new account with their name and account balance. The account number should be generated automatically (consecutive).
I would also like to add the following:
Unfortunately I can't get any further here.
This is how my account.txt file looks like:
1, Max Mustermann, 1000.0
2, Nora Mustermann, 790.0
3, Tomas Mustermann, 400.0
This my coding:
data = {}
with open("Account.txt") as f:
print("Which account do you want to use?")
for folder in f:
list = folder.split(",")
account_number = int(list[0])
name = list[1]
credit = list[2]
data[account_number] = list[1:]
print(f" [{account_number}] {name}")
print(" [+] Create new account")
print(" [0] End")
input_first = int(input(" Your input: "))
if input_first == 0:
print("Thank you and see you again")
exit()
elif input_first == '+':
print("new account")
#From here I get no further, how to use "+" in a input?
while input_first in data:
credit2 = data[input_first][1]
name2 = data[input_first][0]
with open('Account.txt', 'r') as file:
filedata = file.read()
print("\n[1] Deposit\n[2] Withdraw\n")
execution = int(input(" Your Input: "))
#How can I overdraw my account and make it impossible to deposit or withdraw a negative amount?
if execution == 1:
deposit = float(input(" Your deposit: "))
amount_e = float(credit2) + float(deposit)
print(f" The account balance of account{name2} is {amount_e:.2f} $")
filedata = filedata.replace(str(credit2), str(amount_e) + '\n')
elif execution == 2:
payout = float(input(" Your pay out: "))
amount_a = float(credit2) - float(payout)
filedata = filedata.replace(str(credit2), str(amount_a) + '\n')
print(f" The account balance of account{name2} is {amount_a:.2f} $")
with open('Account.txt', 'w') as file:
file.write(filedata)
First of all, you shouldn't store user data in a txt file. It is not encrypted and it's prominent to data getting stolen. Might wanna look into a database like sqlite in case you want to store it locally or MySQL in case you want it to be stored in a server (I recommend SQlite to start off with). It is also way more intuitive to add, edit and remove elements this way than editing a plain txt file.
Regarding the negative values it's pretty easy. You just check if the value is not negative like this:
if execution == 1:
deposit = 0
#Don't proceed until a correct value is entered
while deposit < 0:
try:
deposit = float(input(" Your deposit: "))
except TypeError:
#For handling an error if a non-numeric value is entered
pass
amount_e = float(credit2) + float(deposit)
print(f" The account balance of account{name2} is {amount_e:.2f} $")
filedata = filedata.replace(str(credit2), str(amount_e) + '\n')
To stop a user from withdrawing a value higher than what's currently in its account you just check if the withdrawn value is bigger than the account's.
elif execution == 2:
payout = 0
#Keep asking for a value until the payout is less than the credit
while payout > credit2:
payout = float(input(" Your pay out: "))
amount_a = float(credit2) - float(payout)
# Print a message telling the user the reason
if amount_a < 0:
print("You don't have that much money in your account.")
filedata = filedata.replace(str(credit2), str(amount_a) + '\n')
print(f" The account balance of account{name2} is {amount_a:.2f} $")