How to Write Function in Python

Thanks for sharing!

In all programming and scripting languages, a function is a block of code that only runs when it is called.

We can pass data known as parameters into a function. A function can return data as a result. It saves the time of a developer.

For example, custom functions run every time it called and allowed to run the code in more than one place.

How to Write Function in Python Click To Tweet

Let’s Create a Simple ATM Script

The project is broken into three sections:

  • Collect customer input
  • Calculate the ending balance
  • Display the results to the customer

Tips: The float method only accepts one parameter as well as ignores any left or right white spaces and new line, so it can prevent parameter errors or calculation errors.

Input rule:

D = Deposit
W = Withdrawal
B = Account Balance
Q = Quit

Final Output:

Beginning Balance: 500.50
Deposit Amount: 200
Withdrawal Amount: 100

ATM Script:

import sys
account_balance = float(500.25)

def printbalance():
  print('Your current balance:\n %.2f' %(account_balance))
def deposit(amount):
  print("Deposit was $%.2f, current balance is $%.2f" %(deposit_amount, account_balance + deposit_amount))

def withdrawal(amount):
  if amount > account_balance:
  print("$%.2f is greater than your account balance of $%.2f" %(amount, account_balance))
  else:
    print("Withdrawal amount was $%.2f, current balance is $%.2f" %(amount, account_balance - amount))

if (userchoice == 'D'):
  deposit_amount = float(input("How much would you like to deposit today?\n"))
  deposit(deposit_amount)
elif (userchoice == 'W'):
  withdrawal_amount = float(input("How much would you like to withdraw today?\n"))
  withdrawal(withdrawal_amount)
elif (userchoice == 'B'):
  printbalance()
else:
  print("Thank you for banking with us.")

Explanation line by line

This is an account balance.

import sys
account_balance = float(500.25)

This is a custom function called “printbalance.” This function returns the account balance.

def printbalance():
  print('Your current balance:\n %.2f' %(account_balance))

This is a custom function called “deposit.” This function calculates a new account balance and return deposit amount that user input and calculated the new account balance.

def deposit(amount):
  print("Deposit was $%.2f, current balance is $%.2f" %(deposit_amount, account_balance + deposit_amount))

This is a custom function called “withdrawal.” This function calculates a new account balance. Returns the withdrawal amount and calculates the new account balance.

def withdrawal(amount):

If the withdrawal amount is greater than the account balance,

if amount > account_balance:

return the following message: “withdrawal amount” is greater than your account balance of “account balance”

 print("$%.2f is greater than your account balance of $%.2f" %(amount, account_balance))

If the withdrawal amount is less than or equal to the account balance then calculate a new account balance,

else:

return withdrawal amount that user input and calculated the new account balance.

 print("Withdrawal amount was $%.2f, current balance is $%.2f" %(amount, account_balance - amount))

Ok, now let’s collect customer input. Prompting the user to input the request.

userchoice = input ("What would you like to do?\n")

If a user wants to deposit,

if (userchoice == 'D'):

prompting the user to input the amount of money.

 deposit_amount = float(input("How much would you like to deposit today?\n"))

And then, call the deposit function and send the input amount of money.

 deposit(deposit_amount)

If a user wants to withdrawal,

elif (userchoice == 'W'):

prompting the user to input the amount of money.

 withdrawal_amount = float(input("How much would you like to withdraw today?\n"))

And then, call the withdrawal function and send the input amount of money.

  withdrawal(withdrawal_amount)

If a user wants to know account balance,

elif (userchoice == 'B'):

call the print balance function, no parameters/no input need to send.

  printbalance()

If a user wants to quit,

else:

output the message.

  print("Thank you for banking with us.")

Wrapping Up

I hope you enjoy creating a simple ATM script as much as I enjoyed doing this! You can include these types of projects into your portfolio by creating your own site or simply uploaded to Dropbox and share it with your clients.

Subscribe to Newsletter from Happier Freelancing! No spam, you can unsubscribe anytime.

Leave a Reply