0% found this document useful (0 votes)
19 views8 pages

Import Random

Uploaded by

au699298
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views8 pages

Import Random

Uploaded by

au699298
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

import random

import time
import datetime

# Simulate user accounts


accounts = {
"1234567890": {"balance": 1000, "name": "Alice", "pin": "1234", "transactions": []},
"9876543210": {"balance": 500, "name": "Bob", "pin": "5678", "transactions": []},
}

def generate_otp():
"""Generates a 6-digit OTP."""
return str(random.randint(100000, 999999))

def send_otp(account_number, otp):


"""Simulates sending an OTP."""
print(f"Sending OTP {otp} to account {account_number}...")
time.sleep(1)
print("OTP sent.")
return otp

def verify_otp(sent_otp):
"""Verifies the OTP."""
user_otp = input("Enter the OTP you received: ")
return user_otp == sent_otp

def record_transaction(account_number, transaction_type, amount, success):


"""Records a transaction."""
timestamp = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
transaction = {"type": transaction_type, "amount": amount, "timestamp": timestamp,
"success": success}
accounts[account_number]["transactions"].append(transaction)

def withdraw(account_number, amount):


"""Handles withdrawal."""
if amount <= 0:
print("Invalid withdrawal amount.")
return

if amount > accounts[account_number]["balance"]:


print("Insufficient funds.")
record_transaction(account_number, "withdrawal", amount, False)
return
otp = generate_otp()
sent_otp = send_otp(account_number, otp)

if verify_otp(sent_otp):
accounts[account_number]["balance"] -= amount
print(f"Withdrawal of ${amount} successful.")
print(f"New balance: ${accounts[account_number]['balance']}")
record_transaction(account_number, "withdrawal", amount, True)
else:
print("Incorrect OTP. Transaction cancelled.")
record_transaction(account_number, "withdrawal", amount, False)

def check_balance(account_number):
"""Checks balance."""
print(f"Your current balance is: ${accounts[account_number]['balance']}")

def view_transaction_history(account_number):
"""Views transaction history."""
print("\nTransaction History:")
if not accounts[account_number]["transactions"]:
print("No transactions recorded yet.")
return

for transaction in accounts[account_number]["transactions"]:


print(f"Type: {transaction['type']}, Amount: ${transaction['amount']}, Timestamp:
{transaction['timestamp']}, Success: {'Yes' if transaction['success'] else 'No'}")

def change_pin(account_number):
"""Changes PIN."""
for attempt in range(3):
current_pin = input("Enter your current PIN: ")
if current_pin == accounts[account_number]["pin"]:
while True:
new_pin = input("Enter your new 4-digit PIN: ")
if new_pin.isdigit() and len(new_pin) == 4:
confirm_new_pin = input("Confirm your new PIN: ")
if new_pin == confirm_new_pin:
accounts[account_number]["pin"] = new_pin
print("PIN changed successfully.")
return True
else:
print("New PINs do not match. Please try again.")
else:
print("Invalid PIN. Please enter a 4-digit number.")
break
else:
print(f"Incorrect PIN. Attempts remaining: {2 - attempt}")
print("Too many incorrect PIN attempts. PIN change failed.")
return False

def get_account_number():
"""Gets and validates account number."""
while True:
account_number = input("Enter your account number: ")
if account_number.isdigit() and len(account_number) == 10:
return account_number
else:
print("Invalid account number. Please enter a 10 digit number")

def get_pin(account_number):
"""Gets and verifies PIN."""
for attempt in range(3):
pin = input("Enter your 4-digit PIN: ")
if pin == accounts[account_number]["pin"]:
return True
else:
print(f"Incorrect PIN. Attempts remaining: {2 - attempt}")
print("Too many incorrect PIN attempts. Account locked.")
return False

def get_amount():
"""Gets and validates amount."""
while True:
try:
amount = float(input("Enter the amount: $"))
if amount > 0:
return amount
else:
print("Please enter a positive amount")
except ValueError:
print("Invalid input. Please enter a number.")

if __name__ == "__main__":
print("Welcome to the Bank Transaction System")
account_number = get_account_number()

if account_number not in accounts:


print("Account not found.")
elif get_pin(account_number):
while True:
print("\nChoose an action:")
print("1. Withdraw")
print("2. Check Balance")
print("3. View Transaction History")
print("4. Change PIN")
print("5. Exit")

choice = input("> ")

if choice == "1":
amount = get_amount()
withdraw(account_number, amount)
elif choice == "2":
check_balance(account_number)
elif choice == "3":
view_transaction_history(account_number)
elif choice == "4":
change_pin(account_number)
elif choice == "5":
break
else:
print("Invalid choice.")
else:import random
import time
import datetime

# Simulate user accounts


accounts = {
"1234567890": {"balance": 1000, "name": "Alice", "pin": "1234", "transactions": []},
"9876543210": {"balance": 500, "name": "Bob", "pin": "5678", "transactions": []},
}

def generate_otp():
"""Generates a 6-digit OTP."""
return str(random.randint(100000, 999999))

def send_otp(account_number, otp):


"""Simulates sending an OTP."""
print(f"Sending OTP {otp} to account {account_number}...")
time.sleep(1)
print("OTP sent.")
return otp
def verify_otp(sent_otp):
"""Verifies the OTP."""
user_otp = input("Enter the OTP you received: ")
return user_otp == sent_otp

def record_transaction(account_number, transaction_type, amount, success):


"""Records a transaction."""
timestamp = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
transaction = {"type": transaction_type, "amount": amount, "timestamp": timestamp,
"success": success}
accounts[account_number]["transactions"].append(transaction)

def withdraw(account_number, amount):


"""Handles withdrawal."""
if amount <= 0:
print("Invalid withdrawal amount.")
return

if amount > accounts[account_number]["balance"]:


print("Insufficient funds.")
record_transaction(account_number, "withdrawal", amount, False)
return

otp = generate_otp()
sent_otp = send_otp(account_number, otp)

if verify_otp(sent_otp):
accounts[account_number]["balance"] -= amount
print(f"Withdrawal of ${amount} successful.")
print(f"New balance: ${accounts[account_number]['balance']}")
record_transaction(account_number, "withdrawal", amount, True)
else:
print("Incorrect OTP. Transaction cancelled.")
record_transaction(account_number, "withdrawal", amount, False)

def check_balance(account_number):
"""Checks balance."""
print(f"Your current balance is: ${accounts[account_number]['balance']}")

def view_transaction_history(account_number):
"""Views transaction history."""
print("\nTransaction History:")
if not accounts[account_number]["transactions"]:
print("No transactions recorded yet.")
return

for transaction in accounts[account_number]["transactions"]:


print(f"Type: {transaction['type']}, Amount: ${transaction['amount']}, Timestamp:
{transaction['timestamp']}, Success: {'Yes' if transaction['success'] else 'No'}")

def change_pin(account_number):
"""Changes PIN."""
for attempt in range(3):
current_pin = input("Enter your current PIN: ")
if current_pin == accounts[account_number]["pin"]:
while True:
new_pin = input("Enter your new 4-digit PIN: ")
if new_pin.isdigit() and len(new_pin) == 4:
confirm_new_pin = input("Confirm your new PIN: ")
if new_pin == confirm_new_pin:
accounts[account_number]["pin"] = new_pin
print("PIN changed successfully.")
return True
else:
print("New PINs do not match. Please try again.")
else:
print("Invalid PIN. Please enter a 4-digit number.")
break
else:
print(f"Incorrect PIN. Attempts remaining: {2 - attempt}")
print("Too many incorrect PIN attempts. PIN change failed.")
return False

def get_account_number():
"""Gets and validates account number."""
while True:
account_number = input("Enter your account number: ")
if account_number.isdigit() and len(account_number) == 10:
return account_number
else:
print("Invalid account number. Please enter a 10 digit number")

def get_pin(account_number):
"""Gets and verifies PIN."""
for attempt in range(3):
pin = input("Enter your 4-digit PIN: ")
if pin == accounts[account_number]["pin"]:
return True
else:
print(f"Incorrect PIN. Attempts remaining: {2 - attempt}")
print("Too many incorrect PIN attempts. Account locked.")
return False

def get_amount():
"""Gets and validates amount."""
while True:
try:
amount = float(input("Enter the amount: $"))
if amount > 0:
return amount
else:
print("Please enter a positive amount")
except ValueError:
print("Invalid input. Please enter a number.")

if __name__ == "__main__":
print("Welcome to the Bank Transaction System")
account_number = get_account_number()

if account_number not in accounts:


print("Account not found.")
elif get_pin(account_number):
while True:
print("\nChoose an action:")
print("1. Withdraw")
print("2. Check Balance")
print("3. View Transaction History")
print("4. Change PIN")
print("5. Exit")

choice = input("> ")

if choice == "1":
amount = get_amount()
withdraw(account_number, amount)
elif choice == "2":
check_balance(account_number)
elif choice == "3":
view_transaction_history(account_number)
elif choice == "4":
change_pin(account_number)
elif choice == "5":
break
else:
print("Invalid choice.")
else:
print("Login Failed.")
print("Login Failed.")

You might also like