lab 8
play

Lab 8 Reading, wri0ng files Modules Excep0on Handling Using lists - PDF document

Lab 8 Reading, wri0ng files Modules Excep0on Handling Using lists to solve problems March 13, 2018 Sprenkle - CSCI111 1 Lab 8: Pair Programming Findley Jordan Jordan Findley Parker Margaret Margaret Parker Ryan Chas Chas


  1. Lab 8 • Reading, wri0ng files • Modules • Excep0on Handling • Using lists to solve problems March 13, 2018 Sprenkle - CSCI111 1 Lab 8: Pair Programming Findley Jordan Jordan Findley Parker Margaret Margaret Parker Ryan Chas Chas Ryan Lizzie Robert Robert Lizzie Olivia Anna Anna Olivia Lindsey Ben Ben Lindsey Alison Rachel Rachel Alison Kalady Mary-Frances Mary-Frances Kalady Joseph Andrew Andrew Joseph Harris Davis Davis Harris Chase Ian Ian Chase Same pairing in each table March 13, 2018 Sprenkle - CSCI111 2 1

  2. Pair Programming • Two of you Ø double check problem requirements Ø Push each other: beWer tests, beWer comments Ø Iterate March 13, 2018 Sprenkle - CSCI111 3 Compare Solu0ons words = sentence.split() shorthandList = [] for word in words: shorthandList.append(word[0]) shorthand = "".join(shorthandList) shorthand = shorthand.lower() print("Shorthand is:", shorthand) words = sentence.split() shorthand="" for word in words: shorthand += word[0] shorthand = shorthand.lower() print("Shorthand is:", shorthand) March 13, 2018 Sprenkle - CSCI111 4 2

  3. Compare Solu0ons Both are valid solutions. I’m not sure which is more words = sentence.split() efficient in practice. shorthandList = [] for word in words: However, the solution at left shorthandList.append(word[0]) has more conceptual complexity (appending to a shorthand = "".join(shorthandList) list and then converting to a shorthand = shorthand.lower() string, as opposed to just creating the string). print("Shorthand is:", shorthand) words = sentence.split() shorthand="" In general, look for less for word in words: complex solutions. shorthand += word[0] shorthand = shorthand.lower() print("Shorthand is:", shorthand) March 13, 2018 Sprenkle - CSCI111 5 Comment Example def def encodeLetter(char, key): """Encodes a single character. PRE: Input parameters are a single, lowercase character string (char) and an integer key (between -25 and 25, inclusive) POST: returns the encoded character as a str""" • Does not say who called func0on, where parameters came from, or where returned to • Any code can call the func0on and pass in input from anywhere (e.g., hardcoded, from user input, test func0on, …) • Does not say variable name returned March 13, 2018 Sprenkle - CSCI111 6 3

  4. Review Caesar Cipher • Consider the following solu0ons for char in for in message: asciiVal = ord(char) if if asciiVal == 32: … else else: … for char in for in message: if if char == " ": … else else: … March 13, 2018 Sprenkle - CSCI111 7 Review Caesar Cipher • Consider the following solu0ons for for char in in message: asciiVal = ord(char) if if asciiVal == 32: … else else: … for char in for in message: I know what " " means. if if char == " ": I don’t immediately know … what 32 means. else else: Lesson: prefer words … over numbers. March 13, 2018 Sprenkle - CSCI111 8 4

  5. Review • What are things we can do to lists? • How do we work with files? • What is the structure we use to do excep0on handling? March 13, 2018 Sprenkle - CSCI111 9 CREATING MODULES March 12, 2018 Sprenkle - CSCI111 10 5

  6. Where are Func0ons Defined? • Func0ons can go inside of program script Ø Defined before use/called (if no main main () func0on) Ø Or, below the main main () func0on ( ) • Func0ons can go inside a separate module March 12, 2018 Sprenkle - CSCI111 11 Crea0ng Modules • Modules group together related func0ons and constants • Unlike func0ons, no special keyword to define a module Just a Ø A module is named by its filename Python file! • You’ve used modules in the past Ø graphics.py Ø game.py March 12, 2018 Sprenkle - CSCI111 12 6

  7. Typical Use of Modules • Put your reusable code in a module that can be shared with others • Example: game.py Ø rollDie(sides) Ø rollMultipleDice(numDice, sides) • Call import game in Python interpreter Ø What happened? March 12, 2018 Sprenkle - CSCI111 13 Crea0ng Modules • Then, to call rollDie rollDie func0on Ø game.rollDie(6) • To access a defined constants Ø Example: game.SIDES March 12, 2018 Sprenkle - CSCI111 14 7

  8. Crea0ng Modules • So that our program doesn’t execute code automa0cally when it is imported in a program, at boWom, add if if __name__ == '__main__' : testRollDie() testRollMultipleDice() Not important how this works; just know when to use • Note the sub-directories now listed in the directory March 12, 2018 Sprenkle - CSCI111 15 Benefits of Defining Func0ons in Separate Module • Reduces code in primary driver script • Easier to reuse by impor0ng from a module • Maintains the “black box” Ø Abstrac,on • Isolates tes0ng of func0on • Write “test driver” scripts to test func0ons separately from use in script March 12, 2018 Sprenkle - CSCI111 16 8

  9. Lab 8 Overview • Modules • Reading Files • Wri0ng Files • Excep0on handling • Func0ons/Lists March 13, 2018 Sprenkle - CSCI111 17 9

Recommend


More recommend