Download presentation
Presentation is loading. Please wait.
Published byAdela Miller Modified over 8 years ago
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: 1. Display menu and get choice from user 2. Allow user to check balance 3. Accept deposits (positive numbers only) 4. Accept withdrawals (no more than balance) 5. 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 alance -- check on account balance eposit -- make a deposit to account ithdrawal -- make a withdrawal from account uit Enter selection: w Enter withdrawal amount: $9000 Insufficient funds alance -- check on account balance eposit -- make a deposit to account ithdrawal -- make a withdrawal from account uit Enter selection: w Enter withdrawal amount: $900
10
Sample Output cont. alance -- check on account balance eposit -- make a deposit to account ithdrawal -- make a withdrawal from account uit Enter selection: d Enter deposit amount: $540 alance -- check on account balance eposit -- make a deposit to account ithdrawal -- make a withdrawal from account 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 alance -- check on account balance eposit -- make a deposit to account ithdrawal -- make a withdrawal from account uit Enter selection: q Beginning balance: $1000.00 Total withdrawals: $900.00 Total deposits: $540.00 Ending balance: $640.00
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.