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:
- It should not be possible to overdraw the account.
- It should no longer be possible to deposit or withdraw a negative amount.
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('Konto.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('Konto.txt', 'w') as file:
file.write(filedata)
CodePudding user response:
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')
CodePudding user response:
You can check the value of the deposited or withdrawn amount before moving to the next step. If the deposited amount is less than 0, then tell the user about the issue. Using nested if statements and the break command can get you out of the program.
If you are looking for more specific behavior, please explain what you want to happen in more detail.