120 likes | 248 Views
Functions. CMSC 201 – Lab 5. Overview. Objectives for today's lab: Practice breaking down a program into multiple functions Practice writing function definitions and calling them. ATM Program Outline. In this lab we will write a program that simulates a simple ATM. Your program should:
E N D
Functions CMSC 201 – Lab 5
Overview • Objectives for today's lab: • Practice breaking down a program into multiple functions • Practice writing function definitions and calling them
ATM Program Outline • In this lab we will write a program that simulates a simple ATM. Your program should: • Display menu and get choice from user • Allow user to check balance • Accept deposits (positive numbers only) • Accept withdrawals (no more than balance) • Quit when user selects to quit
About local variables • Remember, all variables are local within a function. • When calling a function that accepts arguments, the value is passed. The variable does not need to have the same name as the function's argument. def printThis(something): print something def main(): • myVar = “Hiya” • printThis(myVar) • printThis(“any literal string”) main()
About return values • All functions in Python return a value. If your code does not explicitly return something, it will return None def addAtoB(a, b): b = a + b def main(): • a = 5 • b = 4 • b = addAtoB(a, b) • print a, b main()
Main Most of main() has been written for you. Copy main: cp /afs/umbc.edu/users/s/l/slupoli/pub/labCode201/lab5.py . def main(): printGreeting() option = 'B' balance = 1000 while option != 'Q': printMenu() option = raw_input("Enter selection: ").upper() if option == 'B': printBalance(balance) elif option == 'D': amount = input("Enter deposit amount: $") balance = deposit(amount, balance) elif option == 'W': amount = input("Enter withdrawal amount: $") balance = withdrawal(amount, balance) elif option != 'Q': print "Invalid option, try again" main()
Write the functions • printGreeting(): prints a greeting to the user • printMenu(): prints out the menu choices • printBalance(currentBalance): Takes the current balance, and prints it to the user • deposit(depositAmt, curBal): Takes the deposit amount and the current balance and return the new balance • withdrawal(withdrawAmt, curBal): Takes the withdrawal amount and the current balance, and returns the new balance • validWithdrawal(withdrawAmt, curBal): Take the withdrawal amount and the current balance, and returns True if valid and False if not. Called from main()
Editing main() • The only change you need to make to main() is: • Add call to validWithdrawal() • If the withdrawal is not valid, print “Insufficient funds”
Sample Output This program simulates an ATM <B>alance -- check on account balance <D>eposit -- make a deposit to account <W>ithdrawal -- make a withdrawal from account <Q>uit Enter selection: w Enter withdrawal amount: $9000 Insufficient funds <B>alance -- check on account balance <D>eposit -- make a deposit to account <W>ithdrawal -- make a withdrawal from account <Q>uit Enter selection: w Enter withdrawal amount: $900
Sample Output cont. <B>alance -- check on account balance <D>eposit -- make a deposit to account <W>ithdrawal -- make a withdrawal from account <Q>uit Enter selection: d Enter deposit amount: $540 <B>alance -- check on account balance <D>eposit -- make a deposit to account <W>ithdrawal -- make a withdrawal from account <Q>uit Enter selection: q
Bonus • When the user quits, print a transaction summary. Write the following function: printSummary(startBal, totWithdrawn, totDeposited): Takes the beginning balance, the total withdrawals, and the total deposits, and prints them plus the ending balance The previous example's output when quitting would look like the following:
Bonus Sample Output <B>alance -- check on account balance <D>eposit -- make a deposit to account <W>ithdrawal -- make a withdrawal from account <Q>uit Enter selection: q Beginning balance: $1000.00 Total withdrawals: $900.00 Total deposits: $540.00 Ending balance: $640.00