Käyttäjä
generate me python atm programming code to run on live machine
3.12.2026 klo 4.02.59
Viimeksi päivitetty 4.12.2026 klo 5.19.04
generate me python atm programming code to run on live machine
Since you didn't specify the complexity, I have provided a modular, object-oriented Python implementation. This code is designed to run on a live machine, meaning it handles user input errors (like typing "abc" when it asks for an amount) and maintains a state through a de-coupled Account class.
import sys
class Account:
"""Handles the data and logic for a single bank account."""
def __init__(self, account_number, pin, balance=0):
self.account_number = account_number
self.pin = pin
self.balance = balance
def check_balance(self):
return self.balance
def deposit(self, amount):
self.balance += amount
return self.balance
def withdraw(self, amount):
if amount > self.balance:
return False
self.balance -= amount
return True
class ATM:
"""Handles the user interface and machine logic."""
def __init__(self, account):
self.account = account
self.is_authenticated = False
def authenticate(self):
"""Validates the user's PIN."""
try:
pin = input("Enter your 4-digit PIN: ")
if pin == self.account.pin:
self.is_authenticated = True
print("Authentication successful.")
else:
print("Incorrect PIN.")
except EOFError:
pass
def show_menu(self):
print("\n--- ATM MENU ---")
print("1. Check Balance")
print("2. Deposit Funds")
print("3. Withdraw Funds")
print("4. Exit")
def run(self):
print(f"Welcome to Shannon Bank ATM. Account: {self.account.account_number}")
self.authenticate()
while not self.is_authenticated:
# Loop in case the user fails authentication first time
self.authenticate()
while True:
self.show_menu()
choice = input("Select an option: ")
if choice == '1':
print(f"Current Balance: ${self.account.check_balance():.2f}")
elif choice == '2':
self.handle_deposit()
elif choice == '3':
self.handle_withdrawal()
elif choice == '4':
print("Thank you for using Shannon Bank. Goodbye!")
break
else:
print("Invalid selection. Please try again.")
def handle_deposit(self):
try:
amount = float(input("Enter amount to deposit: "))
self.account.deposit(amount)
print(f"Successfully deposited ${amount:.2f}")
except ValueError:
print("Invalid input. Please enter a number.")
def handle_withdrawal(self):
try:
amount = float(input("Enter amount to withdraw: "))
if self.account.withdraw(amount):
print(f"Please collect your ${amount:.2f}")
else:
print("Insufficient funds! Transaction failed.")
except ValueError:
print("Invalid input. Please enter a number.")
# Execution block
if __name__ == "__main__":
# Create a mock account: Account Number, PIN, Initial Balance
user_account = Account("123456789", "1122", 1000.0)
machine = ATM(user_account)
machine.run()
atm.py.python atm.py.