Python – May 17 Recap lab Multidimensional list Exceptions Commitment: quiz tomorrow
Lab notes Put delimiters inside (“[ ]”) When you sort a list, there is no return value L.sort()# rather than L = L.sort() In lottery problem, how did you make sure all winning numbers distinct? –Technique also helpful when “dealing” cards. Can check syntax before running: next to run button there is a “check”. Status bar shows result, and cursor taken to error, if any
Multi-D Intuitive, just like other languages. List inside a list [ [ 1, 2 ], [ 8, 6 ] ] Access element with multiple indices: list[3][2] Very often associated with nested loops Applications? –List of lines; list of words on the line –Board game
Examples Sudoku and Reversi
Exceptions Chapter 10 covers in great detail Similar to Java Find out what could go wrong with your code try-except block try : possibly exceptional code except Name_of_exception: what to do in case of problem
Exceptions Common types –ValueError –IOError –IndexError You can raise an exception if you discover something wrong: if value < 0: raise ValueError Another use of “raise”: raise SystemExit is a way out of your program.
Example while True: try: value = input("Enter a positive number") if value <= 0: raise ValueError else: break except ValueError: print "Please follow directions" continue print value