Page 128 -
P. 128

code to protect





                                                  You were to study your program and circle the line or lines of
                                                  code that you think you need to protect. Then, in the space
                                                  provided, you were to state why.


                            data = open('sketch.txt')


                            for each_line in data:
                                     (role, line_spoken) = each_line.split(':', 1)
                                     print(role, end='')                                    These four
                                     print(' said: ', end='')                               lines of code
                                     print(line_spoken, end='')                              all need to be
                                                                                             protected.

                            data.close()





                              If the call to “split()” fails, you don’t want the three “print()”
                               statements executing, so it’s best to protect all four lines of the “if”
                               suite, not just the line of code that calls “split()”.










                      OK. I get that the code                                       Yeah...good point. It’s
                      can be protected from an                                      probably best to ignore
                      error. But what do I do when                                  it, right? I wonder how...
                      an error actually occurs?


















           92    Chapter 3
   123   124   125   126   127   128   129   130   131   132   133