Functions CMSC 201 – Lab 5. Overview Objectives for today's lab:  Practice breaking down a program into multiple functions  Practice writing function.

Slides:



Advertisements
Similar presentations
Chapter 4: Requirements Engineering
Advertisements

Python Mini-Course University of Oklahoma Department of Psychology Lesson 28 Classes and Methods 6/17/09 Python Mini-Course: Lesson 28 1.
Introduction to Computing Science and Programming I
Introduction to Software Testing Chapter 2.6 Graph Coverage for Use Cases Paul Ammann & Jeff Offutt
Use Case Model. C-S 5462 Use case model describes what the user expects the system to do –functional requirements may describe only the functionalities.
USE CASE – ATM EXAMPLE Actors: ATM Customer ATM Operator Use Cases: The customer can withdraw funds from a checking or savings account query the balance.
Adapted from John Zelle’s Book Slides
Sample Problems for Testing For “Program” Level Testing: –Triangle –Next Date –Sales Commission For “System” Level Testing: –ATM system –Currency conversion.
CPSC 333: Foundations of Software EngineeringJ. Denzinger Small Test: Bank account manager System has to run on an automated teller machine. User must.
Problem Solving and Program Design in C (5th Edition) by Jeri R. Hanly and Elliot B. Koffman Chapter 4 (Conditional Statements) © CPCS
CHAPTER 9 DEFINING CLASSES & CREATING OBJECTS Introduction to Computer Science Using Ruby (c) 2012 Ophir Frieder et al.
Conditional Statements Introduction to Computing Science and Programming I.
Example 2.
Unit 211 Requirements Phase The objective of this section is to introduce software system requirements and to explain different ways of expressing these.
Python November 18, Unit 7. So Far We can get user input We can create variables We can convert values from one type to another using functions We can.
Chapter 2 Writing Simple Programs
{ How to Use An ATM A simple tutorial to teach how to use ATM Machines.
McGraw-Hill/Irwin Introduction to QuickBooks Pro, 2004 © 2005 The McGraw-Hill Companies, Inc., All Rights Reserved. Chapter 4 Bank Reconciliation.
People © 2013 The Sleeter Group All rights reserved. Intuit, the Intuit logo and QuickBooks, among others, are registered trademarks of Intuit Inc. Other.
2013.  Reconcile your checking account  Create bank reconciliation reports  Find errors during reconciliation  Correct errors found during reconciliation.
Functions A function is a snippet of code that performs a specific task or tasks. You use a multitude of functions daily when you do such things as store.
More on Functions CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
1 CSC103: Introduction to Computer and Programming Lecture No 13.
Repetition Statements Repeating an Action A specified number of times While a Condition is True Until a Condition is True.
Project 2 Overview Start Part 1 Today Part 2 [Optional] later Due: On or before last class.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 6 Value- Returning Functions and Modules.
STRINGS CMSC 201 – Lab 3. Overview Objectives for today's lab:  Obtain experience using strings in Python, including looping over characters in strings.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 6 Value-Returning.
Sample Problems for Testing
Practice with Lists and Strings CS303E: Elements of Computers and Programming.
Functions Chapter 4 Python for Informatics: Exploring Information
Functions CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
The switch Statement.  Occasionally, an algorithm will contain a series of decisions in which a variable or expression is tested separately for each.
Python uses boolean variables to evaluate conditions. The boolean values True and False are returned when an expression is compared or evaluated.
Chapter 3 New Company Setup for a Merchandising Business Copyright © 2015 McGraw-Hill Education. All rights reserved. No reproduction or distribution.
CSC 110 Using Python [Reading: chapter 1] CSC 110 B 1.
ProgLan Python Session 4. Functions are a convenient way to divide your code into useful blocks, allowing us to: order our code, make it more readable,
1 Graph Coverage (6). Reading Assignment P. Ammann and J. Offutt “Introduction to Software Testing” ◦ Section
Lecture 2 Conditional Statement. chcslonline.org Conditional Statements in PHP Conditional Statements are used for decision making. Different actions.
Learning Intentions Explain what an ATM is and the facilities offered Identify the stages of withdrawing cash from an ATM List the advantages and disadvantages.
More on Functions Intro to Computer Science CS1510 Dr. Sarah Diesburg.
Python Let’s get started!.
Lecture 7: Menus and getting input. switch Multiple-selection Statement switch Useful when a variable or expression is tested for all the values it can.
Functions CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
Check Lingo A personal check is a written form that states a written amount to withdraw directly from the payer’s account.
Today… Python Keywords. Iteration (or “Loops”!) Winter 2016CISC101 - Prof. McLeod1.
Winter 2016CISC101 - Prof. McLeod1 CISC101 Reminders Quiz 3 next week. See next slide. Both versions of assignment 3 are posted. Due today.
COMPUTER PROGRAMMING Year 9 – lesson 1. Objective and Outcome Teaching Objective We are going to look at how to construct a computer program. We will.
Bank Reconciliation Chapter 4. PAGE REF #CHAPTER 4: Bank Reconciliation SLIDE # 2 Objectives Reconcile your checking Create bank reconciliation reports.
Today… Operators, Cont. Operator Precedence Conditional Statement Syntax. Winter 2016CISC101 - Prof. McLeod1.
More on Functions Intro to Computer Science CS1510 Dr. Sarah Diesburg.
Programming Logic and Design Fourth Edition, Comprehensive Chapter 10 Using Menus and Validating Input.
Functions. What is a Function?  We have already used a few functions. Can you give some examples?  Some functions take a comma-separated list of arguments.
CSC 1010 Programming for All Lecture 5 Functions Some material based on material from Marty Stepp, Instructor, University of Washington.
1 C++ Classes and Data Structures Course link…..
Output “Funds not available”
How does it work? What is it made of? What is it made of?
Python Let’s get started!.
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Chapter 4 (Conditional Statements)
Example Programs.
More Selections BIS1523 – Lecture 9.
Validations and Error Handling
CISC101 Reminders Assn 3 due tomorrow, 7pm.
Topics Introduction to Value-returning Functions: Generating Random Numbers Writing Your Own Value-Returning Functions The math Module Storing Functions.
Please use speaker notes for additional information!
Lab4 PROGRAMMING 1 exercise chapter3.
CISC101 Reminders Assignment 3 due today.
Global Variables Created by assignment statement placed at beginning of program and outside all functions Can be accessed by any statement in the program.
Functions, Procedures, and Abstraction
Presentation transcript:

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

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

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

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 alance -- check on account balance eposit -- make a deposit to account ithdrawal -- make a withdrawal from account uit Enter selection: q Beginning balance: $ Total withdrawals: $ Total deposits: $ Ending balance: $640.00