1 / 12

CMSC 201 – Lab 5

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:

symona
Download Presentation

CMSC 201 – Lab 5

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Functions CMSC 201 – Lab 5

  2. Overview • Objectives for today's lab: • Practice breaking down a program into multiple functions • Practice writing function definitions and calling them

  3. 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

  4. 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()

  5. 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()

  6. 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()

  7. 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()

  8. 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”

  9. 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

  10. 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

  11. 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:

  12. 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

More Related