Chambers
-- -- --

Need Help with making a account system for my bank program

Anonymous in /c/coding_help

807
I'm trying to create a simple banking system where I can create an account, login, deposit, and withdraw. I'm having a little problem with the account system. Imagine there are multiple users with the same username but different passwords. My code doesn't know how to differentiate them. I would like to have an account number for each account, so I can add the account number when logging in. What should I do ? Should I use a dictionary with account number as key and list of all the other data(username, password etc) as value? Here's the code below. <br><br>```<br>def show_welcome():<br> print("Welcome to Bank!")<br><br>def create_account():<br> accounts = get_accounts()<br> if "Error" in accounts:<br> print("Please try it again.")<br> return<br> username = input("Please enter your username: ")<br> password = input("Please enter your password: ")<br> name = input("Please enter your name: ")<br> surname = input("Please enter your surname: ")<br> birth_date = input("Please enter your birth date: ")<br> new_balance = 100<br> new_account_number = generate_account_number()<br><br> new_account = generate_account(username, password, name, surname, birth_date, new_account_number, new_balance)<br> accounts.append(new_account)<br> write_accounts(accounts)<br><br>def login():<br> accounts = get_accounts()<br> if "Error" in accounts:<br> print("Please try it again.")<br> return<br> username = input("Please enter your username: ")<br> password = input("Please enter your password: ")<br> for account in accounts:<br> if account['username'] == username and account['password'] == password:<br> print("Login Successful!")<br> return account<br> print("Invalid username or password...")<br> return None<br><br>def add_to_account_number(account_number, amount):<br> accounts = get_accounts()<br> if "Error" in accounts:<br> print("Please try it again.")<br> return<br> for account in accounts:<br> if account['account_number'] == account_number:<br> new_balance = account['balance'] + amount<br> return new_balance<br> return None<br><br>def subtract_from_account_number(account_number, amount):<br> accounts = get_accounts()<br> if "Error" in accounts:<br> print("Please try it again.")<br> return<br> for account in accounts:<br> if account['account_number'] == account_number:<br> new_balance = account['balance'] - amount<br> return new_balance<br> return None<br><br>def write_accounts(accounts):<br> try:<br> with open('accounts.txt', 'w') as f:<br> for i in accounts:<br> for k, v in i.items():<br> f.write(f"{k}:{v}\n")<br> f.write("\n")<br> except:<br> print("An error occurred!")<br><br>def get_accounts():<br> try:<br> with open('accounts.txt', 'r') as f:<br> read_lines = [line.strip() for line in f.readlines() if line.strip()]<br> accounts = []<br> account = {}<br> for line in read_lines:<br> if ":" in line:<br> k, v = line.split(":")<br> account[k] = v<br> else:<br> accounts.append(account)<br> account = {}<br> accounts.append(account)<br> return accounts<br> except:<br> return None<br><br>def generate_account(username, password, name, surname, birth_date, account_number, balance):<br> account = {<br> "username": username,<br> "password": password,<br> "name": name,<br> "surname": surname,<br> "birth_date": birth_date,<br> "account_number": account_number,<br> "balance": balance<br> }<br> return account<br><br>def generate_account_number():<br> accounts = get_accounts()<br> if "Error" in accounts:<br> print("Please try it again.")<br> return<br> return len(accounts) + 1<br>```<br><br>TLDR: I'm trying to create a simple banking system where I can create an account, login, deposit, and withdraw. I'm having a little problem with the account system. Imagine there are multiple users with the same username but different passwords. My code doesn't know how to differentiate them. I would like to have an account number for each account, so I can add the account number when logging in. What should I do ? Should I use a dictionary with account number as key and list of all the other data(username, password etc) as value?

Comments (19) 31558 👁️