Day 2 ยท Day 2: Control Flow
ATM System
Build a command-line ATM simulation with multiple operations.
Requirements
1. Set initial balance
2. Show menu: Check Balance, Deposit, Withdraw, Exit
3. Validate amounts (positive numbers only)
4. Prevent overdraft
5. Show transaction history
2. Show menu: Check Balance, Deposit, Withdraw, Exit
3. Validate amounts (positive numbers only)
4. Prevent overdraft
5. Show transaction history
balance = 1000.00
transactions = []
def show_menu():
print("\n" + "="*30)
print(" WELCOME TO PyATM")
print("="*30)
print("1. Check Balance")
print("2. Deposit")
print("3. Withdraw")
print("4. Transaction History")
print("5. Exit")
print("="*30)
while True:
show_menu()
choice = input("Select option (1-5): ").strip()
if choice == "1":
print(f"\nCurrent Balance: ${balance:.2f}")
elif choice == "2":
try:
amount = float(input("Deposit amount: $"))
if amount <= 0:
print("Amount must be positive!")
else:
balance += amount
transactions.append(f"Deposit: +${amount:.2f}")
print(f"Deposited ${amount:.2f}. New balance: ${balance:.2f}")
except ValueError:
print("Invalid amount!")
elif choice == "3":
try:
amount = float(input("Withdraw amount: $"))
if amount <= 0:
print("Amount must be positive!")
elif amount > balance:
print(f"Insufficient funds! Balance: ${balance:.2f}")
else:
balance -= amount
transactions.append(f"Withdrawal: -${amount:.2f}")
print(f"Withdrew ${amount:.2f}. New balance: ${balance:.2f}")
except ValueError:
print("Invalid amount!")
elif choice == "4":
if not transactions:
print("\nNo transactions yet.")
else:
print("\n--- Transaction History ---")
for t in transactions:
print(f" {t}")
elif choice == "5":
print("\nThank you for using PyATM. Goodbye!")
break
else:
print("Invalid option!")
โก 50 XP on completion
โ Back to Day 2
Your Code
Output
Click "Run" to execute your code...
โณ Loading Python runtime (first run may take a few seconds)...
Log in to mark this project complete and earn XP.