In this Python tutorial, learn the basic, common functions when using an ATM machine. This Python tutorial will give a basic overview on creating a class with methods and objects while implementing loops such as while loops and for loops, and if statements.

Note: This Python tutorial is implemented in Python IDLE (Python GUI) version 3.7.

ATM Function Requirements

The first step is to brainstorm what basic transactions are completed at an ATM?

Some of the functions that one can complete at an ATM are below:

Input user pin for authentication

Check account balance

Deposit funds

Withdraw funds

Create random generated transaction id

Account interest rate and monthly accrued interest rate

Import Python Module Random

In order to complete the above ATM, we will need to import one Python module; random.

Python Random Module

What does the Random package do in Python?

The Random module allows a program to create random numbers by using the random.randint() function. If we wanted a random integer, we can use the randint function and it will accept two parameters of numbers (lowest and highest).

1 import random

Create Class and Define Functions

What is a class?

A class is used for creating objects. By creating objects, the objects have variables and a behavior that’s associated with them. Since we are using Python in this tutorial, a class is created by a keyword class. Once the class is created, the object within the class will then be called the instance of the class.

How to define a function?

Now we take the requirements that we created from the above and create functions. We can define the functions to provide the given functionality of the program. The function blocks are started with def and followed with the function name and parenthesis, such as def getId(self).

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 import random class Account : # Construct an Account object def __init__ ( self , id , balance = 0 , annualInterestRate = 3.4 ) : self . id = id self . balance = balance self . annualInterestRate = annualInterestRate def getId ( self ) : return self . id def getBalance ( self ) : return self . balance def getAnnualInterestRate ( self ) : return self . annualInterestRate def getMonthlyInterestRate ( self ) : return self . annualInterestRate / 12 def withdraw ( self , amount ) : self . balance -= amount def deposit ( self , amount ) : self . balance += amount def getMonthlyInterest ( self ) : return self . balance * self . getMonthlyInterestRate ( )

Define the main() Function

We must create the main() function because it’s only executed when the Python program is executed. Also, we could import a Python program as a module, but the main() method will not execute. The entry point of any program is the main() function, but the interpreter of python will execute the source file code sequentially. In addition, it will not call any method if it’s not within the code. This is why the main() method has a technique, so that the main() method will be executed when the program is executed directly and not when the module is imported.

By creating the main() method, we will use a range to have all users to enter a 4-digit pin to access their account.

1 2 3 4 5 6 def main ( ) : # Creating accounts accounts = [ ] for i in range ( 1000 , 9999 ) : account = Account ( i , 0 ) accounts . append ( account )

ATM Process Creation Using while True

We will use the while True loop because it will loop forever. The while statement will take an expression and execute the body of the loop while the expression is equal to ‘boolean’ of True. As long as the loop stays True, the loop will indefinitely loop.

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 # ATM Processes while True : # Reading id from user id = int ( input ( "

Enter account pin: " ) ) # Loop till id is valid while id < 1000 or id > 9999 : id = int ( input ( "

Invalid Id.. Re-enter: " ) ) # Iterating over account session while True : # Printing menu print ( "

1 - View Balance \t 2 - Withdraw \t 3 - Deposit \t 4 - Exit " ) # Reading selection selection = int ( input ( "

Enter your selection: " ) ) # Getting account object for acc in accounts : # Comparing account id if acc . getId ( ) == id : accountObj = acc break # View Balance if selection == 1 : # Printing balance print ( accountObj . getBalance ( ) ) # Withdraw elif selection == 2 : # Reading amount amt = float ( input ( "

Enter amount to withdraw: " ) ) ver_withdraw = input ( "Is this the correct amount, Yes or No ? " + str ( amt ) + " " ) if ver_withdraw == "Yes" : print ( "Verify withdraw" ) else : break if amt < accountObj . getBalance ( ) : # Calling withdraw method accountObj . withdraw ( amt ) # Printing updated balance print ( "

Updated Balance: " + str ( accountObj . getBalance ( ) ) + " n" ) else : print ( "

You're balance is less than withdrawl amount: " + str ( accountObj . getBalance ( ) ) + " n" ) print ( "

Please make a deposit." ) ; # Deposit elif selection == 3 : # Reading amount amt = float ( input ( "

Enter amount to deposit: " ) ) ver_deposit = input ( "Is this the correct amount, Yes, or No ? " + str ( amt ) + " " ) if ver_deposit == "Yes" : # Calling deposit method accountObj . deposit ( amt ) ; # Printing updated balance print ( "

Updated Balance: " + str ( accountObj . getBalance ( ) ) + " n" ) else : break elif selection == 4 : print ( "nTransaction is now complete." ) print ( "Transaction number: " , random . randint ( 10000 , 1000000 ) ) print ( "Current Interest Rate: " , accountObj . annualInterestRate ) print ( "Monthly Interest Rate: " , accountObj . annualInterestRate / 12 ) print ( "Thanks for choosing us as your bank" ) exit ( ) # Any other choice else : print ( "nThat's an invalid choice." ) # Main function main ( )

Python ATM Program Output

The below is the above code combined to provide the below output of the Python ATM program.

I hope this Python tutorial on creating an ATM program for checking account balance, withdrawing funds, and depositing funds was helpful.