Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save Wangarijane/78680ed6d9bb8cbb00c555f057fd46d3 to your computer and use it in GitHub Desktop.

Select an option

Save Wangarijane/78680ed6d9bb8cbb00c555f057fd46d3 to your computer and use it in GitHub Desktop.
Complete the missing ATM features using functions, lists, dictionaries, and while loops. Do NOT rewrite existing code. Only complete the required functions.
#### ADVANCED ATM SIMULATOR ####
"""
FEATURES
1. Multiple users with unique account numbers and PINs
2. Pin verifications (3 trials before account lock)
3. Balance inquiry
4. Cash withdrawal with balance checks
5. Cash deposit
6. Fund transfer
7. Transaction history
8. Change pin
9. Exit Safely
"""
# ================================
# STORE USERS IN A LIST OF DICTIONARIES
# ================================
users = [
{"username": "John", "full_name": "John Doe", "account_number": "1234567890", "pin": "1234", "balance": 10000.0, "transaction_history": []},
{"username": "Ayomide", "full_name": "Ayomide Smith", "account_number": "0987654321", "pin": "5678", "balance": 15000.0, "transaction_history": []},
{"username": "Samuel", "full_name": "Samuel Johnson", "account_number": "1122334455", "pin": "4321", "balance": 20000.0, "transaction_history": []},
{"username": "Bob", "full_name": "Bob Brown", "account_number": "5566778899", "pin": "8765", "balance": 5000.0, "transaction_history": []},
{"username": "Charlie", "full_name": "Charlie Davis", "account_number": "6677889900", "pin": "2468", "balance": 8000.0, "transaction_history": []}
]
# ================================
# FUNCTIONS
# ================================
# Function to find a user by username
def find_user(username):
for user in users:
if user["username"].lower() == username.lower():
return user
return None
def login():
entered_username = input("Enter your username: ")
user_info = find_user(entered_username)
if user_info is None:
print("User not found!")
return login()
trials = 3
while trials > 0:
pin = input("Enter your pin: ")
if pin == user_info["pin"]:
print("Login Successful!\n")
return user_info
else:
trials -= 1
print("Incorrect Pin. Attempts left:", trials)
print("Too many failed attempts. Account locked.")
return login()
def check_balance(user_info):
print(f"Current Balance: N{user_info['balance']}")
def deposit(user_info):
amount = input("Enter amount to deposit: ")
if amount.isdigit():
amount = int(amount)
if amount > 0:
user_info["balance"] += amount
user_info["transaction_history"].append(f"Deposited N{amount}")
print("Deposit successful!")
else:
print("Amount must be greater than 0")
else:
print("Invalid amount")
# ================================
# 🧠 TASKS (COMPLETED)
# ================================
# TASK 1:
# Create a withdraw(user_info) function
def withdraw(user_info):
amount = input("Enter amount to withdraw: ")
if amount.isdigit():
amount = int(amount)
if amount > 0:
if amount <= user_info["balance"]:
user_info["balance"] -= amount
user_info["transaction_history"].append(f"Withdrew N{amount}")
print("Withdrawal successful!")
else:
print("Insufficient balance!")
else:
print("Amount must be greater than 0")
else:
print("Invalid amount")
# TASK 2:
# Create a transfer(user_info) function
def transfer(user_info):
receiver_username = input("Enter receiver's username: ")
# Prevent transferring to oneself
if receiver_username.lower() == user_info["username"].lower():
print("You cannot transfer money to your own account.")
return
receiver_info = find_user(receiver_username)
if receiver_info is None:
print("Receiver not found!")
return
amount = input("Enter amount to transfer: ")
if amount.isdigit():
amount = int(amount)
if amount > 0:
if amount <= user_info["balance"]:
# Deduct from sender and add to receiver
user_info["balance"] -= amount
receiver_info["balance"] += amount
# Update transaction history for both parties
user_info["transaction_history"].append(f"Transferred N{amount} to {receiver_info['username']}")
receiver_info["transaction_history"].append(f"Received N{amount} from {user_info['username']}")
print("Transfer successful!")
else:
print("Insufficient balance!")
else:
print("Amount must be greater than 0")
else:
print("Invalid amount")
# TASK 3:
# Create a change_pin(user_info) function
def change_pin(user_info):
new_pin = input("Enter new 4-digit PIN: ")
if new_pin.isdigit() and len(new_pin) == 4:
user_info["pin"] = new_pin
print("PIN changed successfully!")
else:
print("Invalid PIN format. Please ensure it is exactly 4 digits.")
# TASK 4:
# Create a view_transaction_history(user_info) function
def view_transaction_history(user_info):
history = user_info["transaction_history"]
if not history:
print("No transactions yet")
else:
print("\n--- Transaction History ---")
for i, transaction in enumerate(history, 1):
print(f"{i}. {transaction}")
print("---------------------------")
# ================================
# MAIN PROGRAM
# ================================
def main():
print("======== Welcome to Optimum Bank ATM ========")
current_user = login()
if current_user:
while True:
print("\n====== MAIN MENU ======")
print("1. Check Balance")
print("2. Deposit")
print("3. Withdraw")
print("4. Transfer")
print("5. Change Pin")
print("6. View Transaction History")
print("7. Exit")
choice = input("Select option (1-7): ")
if choice == "1":
check_balance(current_user)
elif choice == "2":
deposit(current_user)
elif choice == "3":
withdraw(current_user)
elif choice == "4":
transfer(current_user)
elif choice == "5":
change_pin(current_user)
elif choice == "6":
view_transaction_history(current_user)
elif choice == "7":
print("Thank you for using Optimum Bank ATM.")
break
else:
print("Invalid option!")
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment