Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.

Slides:



Advertisements
Similar presentations
Programming Logic and Design Eighth Edition
Advertisements

Introduction to Flowcharting
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 4: Making Decisions.
Commercial Data Processing Lesson 3: Data Validation.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 1- 1.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Java Programming Practice.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 18 Indexing Structures for Files.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design First Edition by Tony Gaddis.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 5: Looping by Tony.
11.3 Function Prototypes A Function Prototype contains the function’s return type, name and parameter list Writing the function prototype is “declaring”
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 4- 1.
Copyright © 2012 Pearson Education, Inc. Chapter 4: Making Decisions.
Chapter 2: Input, Processing, and Output
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5 Part 1 Conditionals and Loops.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Adapted from slides by Marie desJardins
REPETITION STRUCTURES. Topics Introduction to Repetition Structures The while Loop: a Condition- Controlled Loop The for Loop: a Count-Controlled Loop.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Looping Exercises Deciding Which Loop to Use At this.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 6 Value-Returning.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 3 Simple.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 2 Input,
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design First Edition by Tony Gaddis.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Exam scores by range 3-1.
Copyright © 2012 Pearson Education, Inc. Chapter 4: Making Decisions.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 4 Decision.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Chapter 4 Applications of the Derivative.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 1 Functions.
Programming Logic and Design Sixth Edition Chapter 5 Looping.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 8 Working.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
An Object-Oriented Approach to Programming Logic and Design Fourth Edition Chapter 4 Looping.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 4: Making Decisions.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Alice: A Visual Introduction to Programming Third Edition.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 4: Making Decisions.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 4 Making Decisions.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Data Validation while loops. Data validation Programs get input data from different sources All data should be ‘validated’ before the program tries to.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 5 Repetition.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 5 Repetition Structures.
CSC 1010 Programming for All Lecture 4 Loops Some material based on material from Marty Stepp, Instructor, University of Washington.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 2 Input,
Chapter Topics 2.1 Designing a Program 2.2 Output, Input, and Variables 2.3 Variable Assignment and Calculations 2.4 Variable Declarations and Data Types.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Alice: A Visual Introduction to Programming Third Edition.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 4: Making Decisions 1.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
Copyright © 2013 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Third Edition by Tony Gaddis.
Programming Logic and Design Fourth Edition, Comprehensive Chapter 10 Using Menus and Validating Input.
Copyright © 2011 Pearson Education, Inc. Publishing as Prentice Hall Process Specifications and Structured Decisions Systems Analysis and Design, 8e Kendall.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
Copyright © 2013 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Third Edition by Tony Gaddis.
Topics Introduction to Repetition Structures
Topics Introduction to Repetition Structures
Little work is accurate
Patterns to KNOW.
Topics The if Statement The if-else Statement Comparing Strings
Chapter 7: Input Validation
Topics Introduction to Repetition Structures
Presentation transcript:

Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis Chapter 7: Input Validation

Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1-2 Chapter Topics 7.1 Garbage In, Garbage Out 7.2 The Input Validation Loop 7.3 Defensive Programming

Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Garbage In, Garbage Out If a program reads bad data as input, it will produce bad data as output –Programs should be designed to accept only good data –Input Validation All input should be inspected before processing If it’s invalid, it should be rejected and the user should be prompted to enter the correct data

Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Garbage In, Garbage Out

Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley The Input Validation Loop Input validation is commonly done with a loop that iterates as long as input is bad Figure 7-1 Logic containing an input validation loop

Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley The Input Validation Loop Python example: another = 'y' # Process one or more items. while another == 'y' or another == 'Y': print "AGAIN" # Do this again? another = raw_input('Do you have another item? ' + '(Enter y for yes): ')

Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley The Input Validation Loop Priming read is the first input to be tested // Get a test result Display “Enter a test score.” Input score //Make sure it is not lower than 0. While score 100 Display “ERROR: The score cannot be less than 0 ” Display “or greater than 100.” Display “The the correct score.” Input score End While

Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley The Input Validation Loop Priming read is the first input to be tested Python example: #Get a test result score = int(raw_input("Enter a test score: ")) #Make sure it is not lower than 0. while score 100: print "ERROR: The score cannot be less than 0" print "or greater than 100" score = int(raw_input("Enter a test score: "))

Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley The Input Validation Loop Writing Validation Functions –For complex validation, it is recommended to write a function. –This process can make the code look cleaner Validating String Input –Some strings must be validated such as those programs that ask for a specific string input like “yes” –Or programs that specify a string to be a specific length like password validation

Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Defensive Programming Input validation is defensive programming –The practice of anticipating both obvious and unobvious errors that can happen Types of errors to consider –Empty input, where a user accidentally hits enter before entering data –The user enters the wrong type of data

Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Defensive Programming Common errors to be aware of –State abbreviations should be 2-character strings –Zip codes should be in the proper format of 5 or 9 digits –Hourly wages and salary amounts should be numeric values and within ranges –Dates should be checked –Time measurements should be checked –Check for reasonable numbers

Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1-12 # Global constants BASE_HOURS = 40 # Base hours per week OT_MULTIPLIER = 1.5 # Overtime multiplier def main(): hours_worked = int(raw_input("Enter number of hours worked: ")) # Get the hours worked and the hourly pay rate. pay_rate = float(raw_input('Enter the hourly pay rate: ')) # Calculate and display the gross pay. if hours_worked > BASE_HOURS: calc_pay_with_OT(hours_worked, pay_rate) else: calc_regular_pay(hours_worked, pay_rate) # The calc_pay_with_OT function calculates pay with # overtime. It accepts the hours worked and the hourly # pay rate as arguments. The gross pay is displayed. def calc_pay_with_OT(hours, rate): # Calculate the number of overtime hours worked. overtime_hours = hours - BASE_HOURS #Calculate the amount of overtime pay. overtime_pay = overtime_hours * rate * OT_MULTIPLIER #Calculate the gross pay. gross_pay = BASE_HOURS * rate + overtime_pay # Display the gross pay. print 'The gross pay is $%.2f.' % gross_pay # The calc_regular_pay function calculates pay with no overtime. It accepts the hours worked and the hourly # pay rate as arguments. The gross pay is displayed. def calc_regular_pay(hours, rate): # Calculate the gross pay. gross_pay = hours * rate # Display the gross pay. print 'The gross pay is $%.2f.' % gross_pay # Call the main function. main()