-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathatm_project.py
More file actions
79 lines (62 loc) · 2.33 KB
/
atm_project.py
File metadata and controls
79 lines (62 loc) · 2.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import logging
from datetime import datetime
# Sample atm pseudocode
# 1. Start
# 2. Initialize current_balance
# 3. Display menu options
# 4. Get user choice
# 5. If choice is withdrawal:
# a. Get withdrawal amount
# b. Call atm_withdrawal(current_balance, withdrawal_amount)
# 6. If choice is deposit:
# a. Get deposit amount
# b. Call atm_deposit(current_balance, deposit_amount)
# 7. End
# 1. Set-up a professional logging
logging.basicConfig(
filename="atm_transactions.log",
level=logging.INFO,
format="%(asctime)s: %(message)s",
datefmt="%Y-%m-%d %H:%M:%S",
)
# ATM withdrawal function real code
def atm_withdrawal(balance):
"""Handles ATM withdrawal logic and returns the updated balance."""
try:
# Prompt user inside the function since we need fresh input
amount_str = input("\nEnter amount to withdraw: ")
amount = float(amount_str)
# 2. Logical checks
if amount <= 0:
print("Invalid amount. Please enter a positive number.")
return balance
if amount > balance:
print(f"Insufficient funds. Your current balance is ${balance:.2f}")
return balance
# 3. Successful transaction
new_balance = balance - amount
print(f"Withdrawal successful. New balance: KES{new_balance:.2f}")
# This logs using the logging module instead of manual file writing
logging.info(f"Withdrawal of {amount}. New balance: {new_balance}")
return new_balance
except ValueError:
# 4. Handle non-numeric input (e.g., if user types "abc") just for better error handling
print("Error: Please enter a valid numerical amount.")
return balance
# Example usage / Perform the ATM Transaction
def main():
current_balance = 5000.0
print("--- Welcome to my 2026 Python ATM ---")
while True:
print(f"\nCurrent Balance: KES{current_balance:.2f}")
choice = input("1. Withdraw\n2. Exit\nSelect option: ")
if choice == "1":
# 5. Capture the return value to update the global balance
current_balance = atm_withdrawal(current_balance)
elif choice == "2":
print("Thank you for using our ATM. Goodbye!")
break
else:
print("Invalid selection.")
if __name__ == "__main__":
main()