Hands-On Coding II

Get some hands-on practice with the concepts we have learned so far.

In this project lesson, we will use the knowledge about Python we have gained so far and make an ATM system for a single person. The ATM will contain the following functions:

  1. Balance check

  2. Cash deposit

  3. Cash withdrawal

Defining the main function

It is a good idea to define the main() function in the early stages of the project, as it allows us to build the project in a modular, easy-to-follow, and easy-to-fix manner. Let's start off by defining the main() function and populating it with the required variables. No worries if we define all of the variables required for the project; we can just define the ones that we can think of right now and later add new ones if required.

For the ATM system that contains data for a single person, we need to define the name of the person, their account balance, and a PIN for security. Please define the main() function in the space provided below and define the required variables.

After we take the input, the program does nothing else. So, to indicate that the program has ended, we can also print a message at the end of the main() function.

def main(): # Definfing the main function.
  # Define the required varaibles here.
  
  print("Program ended.") # A message for when the program ends.

main() # Calling the main function to initialize the program.
Defining the main() function and populating it with the required variables
Show Solution

Verify the user

Now that we have saved the username and a PIN corresponding to it, we can ask the user to provide their credentials and match them against the ones we have saved to give them access to their account.

A starter code is provided to you below, and a few to-dos have been marked. Complete this code by implementing the following logic:

  • Ask the user for their name.

  • If the name is not what is stored in the system, display an appropriate error message.

  • If the name matches the one saved in the system, ask them for their PIN.

  • If the PINs do not match, display an appropriate error message.

  • If the PIN is correct as well, display the main menu by calling the display_main_menu() functoin.

def main():
  user_name = "John"
  balance = 1000
  saved_pin = "1245"

  input_name = input("Please enter user name: ")
  if ...: # TODO1: Write the appropriate condition.
    print("User not found.")

  else:
    input_pin = input("Enter pin code: ")
    if ...: # TODO2: Write the appropriate condition.
      print("Pin invalid")
    
    else:
      # TODO3: Display the main menu by calling the appropriate function.
      ...

def display_main_menu():
  print("Welcome to the ATM. Please select your desired option.")
  print("1. Check Balance | 2. Cash Deposit | 3. Cash withdrawal | 4. Exit")

main()
Adding conditionals for user name verification
Show Solution

Operating in a loop

From the menu options, we can see that there are three options to perform some action with the account and an option to exit the ATM system. The user may want to use more than one of the options provided in the menu, so the program should only exit when the user chooses to by inputting 4.

To implement this, we will:

  • Initialize an infinite loopA loop whose execution condition is always 'True'. Such a loop will keep on executing unless a breaking statement is called from inside the loop. by setting the condition of the while loop to True.

  • Implement the conditionals to cater to the options shown in the main menu.

  • Break out of the loop when the user inputs 4.

Coding hints

  • The input function takes whatever the user writes as a string. Hence, it is a good idea to compare it with strings when we want to check the input.

  • In the conditional, if you want to write the code at a later stage, you can use the keyword pass or ellipses (...) as temporary placeholders.

  • We should consider the condition that the user inputs something other than what we expected. Can you think of a way to do that?

def main():
  user_name = "John"
  balance = 1000
  saved_pin = "1245"

  input_name = input("Please enter user name: ")
  if input_name != user_name:
    print("User not found.")
  
  else:
    input_pin = input("Enter pin code: ")
    if input_pin != saved_pin:
      print("Pin invalid")
    
    else:
      display_main_menu()
      
      choice = input("Desired option: ")
      while True:
        if choice == '1':
          print(balance)
        # TODO: Implement the rest of the conditions here.

        choice = input("Desired option: ")
      
  print("Program ended.")

def display_main_menu():
  print("Welcome to the ATM. Please select your desired option.")
  print("1. Check Balance | 2. Cash Deposit | 3. Cash withdrawal | 4. Exit")

main()
Adding loop for continuous execution
Show Solution

Implementing functions

By this point, we have the main skeleton of the whole program that we are trying to make. We have a loop with the required conditions within, and it ends when the user wants it to. The termination of loops is accomplished by using the break keyword. Whenever Python encounters break inside a loop, it immediately breaks out of the loop and executes the line immediately after it. So, if the input is 4, we execute the break statement and it will get the job done!

Now, we just need to implement the required functionalities displayed in the main menu. One of them was easy to implement (balance check), and we implemented it right away. The other two, however, will require a good portion of code to implement. For these potions of code, we can make separate functions and then simply call those functions when the conditions are right. Let's make these functions one by one.

Cash deposit

In the widget below, we have provided a skeleton for the main function, fill it up with the required code to implement the required functionality.

The functionality required is as follows:

  • Ask the user for the amount that they want to deposit.

  • Convert the input from a string to a number.

  • Add the amount asked for in the balance.

  • Return the updated amount to the main() function.

Coding hint

At this point, we can assume that the user will input only a number when asked for the balance, and converting the type won't raise any error.

def main():
  user_name = "John"
  balance = 1000
  saved_pin = "1245"

  input_name = input("Please enter user name: ")
  if input_name != user_name:
    print("User not found.")
  
  else:
    input_pin = input("Enter pin code: ")
    if input_pin != saved_pin:
      print("Pin invalid")
    
    else:
      display_main_menu()
      
      choice = input("Desired option: ")
      while True:

        if choice == '1':
          print(balance)
        elif choice == '2':
          balance = deposit(balance) # Calling the deposit function and updating the balance.
        elif choice == '3':
          ...
        elif choice == '4':
          break
        else:
          print("Invalid option.")

        choice = input("Desired option: ")
      
  print("Program ended.")

def display_main_menu():
  print("Welcome to the ATM. Please select your desired option.")
  print("1. Check Balance | 2. Cash Deposit | 3. Cash withdrawal | 4. Exit")

def deposit(balance):
  # Write your code to fulfill the requriements here.
  print("New balance: ", balance)

main()
Adding cash deposit functionality

Show Solution

Cash withdrawal

Yay, we're almost there! Just one more step to go. Our final task is to implement the cash withdrawal functionality. The functionality can be broken down into the following steps:

  • Ask the user for the amount they want to withdraw.

  • Convert the input from a string to a number.

  • Check if this amount can be deducted from the available balance.

  • Subtract this amount from the balance if required.

  • Display a message if the amount can not be deducted from the available balance.

  • Return the updated amount to the main() function.

Coding hint

Again, we can assume that the user will input only a number when asked for the amount to withdraw, and converting the type won't raise any error.

def main():
  user_name = "John"
  balance = 1000
  saved_pin = "1245"

  input_name = input("Please enter user name: ")
  if input_name != user_name:
    print("User not found.")
  
  else:
    input_pin = input("Enter pin code: ")
    if input_pin != saved_pin:
      print("Pin invalid")
    
    else:
      display_main_menu()
      
      choice = input("Desired option: ")
      while True:

        if choice == '1':
          print(balance)
        elif choice == '2':
          balance = deposit(balance)
        elif choice == '3':
          balance = withdraw(balance) # Calling the withdraw function and updating the balance.
        elif choice == '4':
          break
        else:
          print("Invalid option.")

        choice = input("Desired option: ")
      
  print("Program ended.")

def display_main_menu():
  print("Welcome to the ATM. Please select your desired option.")
  print("1. Check Balance | 2. Cash Deposit | 3. Cash withdrawal | 4. Exit")

def deposit(balance):
  deposit_amount = int(input("Enter amount to deposit: "))
  balance = balance + deposit_amount
  print("New balance: ", balance)
  return balance

def withdraw(balance):
  # Write your code to fulfill the requriements here.
  print("Remaining balance: ", balance)
  return balance

main()
Adding cash withdrawal functionality
Show Solution