How do I add a function or if-else statement that restricts account balance, it should have at least 5000 in their account

Question:

Is there any other way we can insert an if-else statement or a function that restricts withdrawal from account and sets the minimum amount the account should have specifically "5000" or else the loop will break or restricts withdrawal from user.

I recently did add an if-else statement in my simple atm ui that sets the maximum the user could withdraw "100k" and breaks if the user withdraws greater than the max amount.

Heres the code:

import time
print("Welcome to OtnisBank")
print("Please insert your card...")
time.sleep(5)
pwd = 1234
pin = int(input("Please enter your security pin: "))
bal = 737000
transaction_Num = 20221000114
while True:

    if pin == pwd:
        print("""
        ********************************************         
                    1) Balance
                    2) Withdraw
                    3) Deposit
                    4) Cancel Transaction       
        ********************************************        
             
            """)
        try:
            opt = int(input("Please choose your transaction "))
        except:
            print("Invalid input, input must be an integer.")

        if opt == 1:
            print(f"Your current balance is ₱{bal}.00")

        if opt == 2:
            print("Maximum of 100k per transaction")
            withdraw = int(input("Please enter amount: "))
            if withdraw > 100000:
                print("Invalid amount, maximum of 100k per transaction.")
            else:
                bal = bal - withdraw
                print(f"₱{withdraw}.00 is withdrawn from your account...")
                print(f"Your updated balance is ₱{bal}.00")

        if opt == 3:
            deposit = int(input("How much money would you like to deposit?: "))
            bal = bal + deposit
            print(f"₱{deposit}.00 is deposited unto your account...")
            print(f"Your updated balance is ₱{bal}.00")

        if opt == 4:
            print("Transaction success...")
            print(f"Transaction number: {transaction_Num + 1}")
            print(
                "Thank you for choosing our bank. Invest in the red, it's in your best interest.")
            break
    else:
        print("Please enter a valid pin and try again.")
        break

Is there any other way we can insert a block of code that restricts account’s minimum balance, it must have at least 5000 to be left in the account or else it will be deleted or something. By the way "bal" variable is global.

Answers:

I’m not sure I understand 100% what you are trying to do, but with this code, you can make sure, that the customer must have 5000 left on the account after withdrawal:

        if withdraw > 100000:
            print("Invalid amount, maximum of 100k per transaction.")
        elif (bal - withdraw) < 5000:
            print("Invalid amount, You must have at least 5000 left on your account.")
        else:
            bal = bal - withdraw
            print(f"₱{withdraw}.00 is withdrawn from your account...")
            print(f"Your updated balance is ₱{bal}.00")
Answered By: user56700
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.