Difference between revisions of "CSC111 Exercises with Exceptions: try/except"

From dftwiki3
Jump to: navigation, search
(Code in need of try/except statements)
(Code)
Line 64: Line 64:
  
 
</source>
 
</source>
 +
<onlydft>
 +
==Solution==
 +
<br />
 +
<source lang="python">
 +
def example1():
 +
    x = eval( input( "enter a number: " ) )
 +
    y = eval( input( "enter another number: " ) )
 +
    try:
 +
        print( x, '/', y, '=', x/y )
 +
    except ZeroDivisionError:
 +
        print( "Cannot divide by 0" )
 +
 +
def example2( L ):
 +
    print( "\n\nExample 2" )
 +
    sum = 0
 +
    for i in range( len( L ) ):
 +
        try:
 +
            sum = sum + L[i]
 +
        except TypeError:
 +
            pass
 +
           
 +
    print( "sum = ", sum )
 +
 +
def example3( L ):
 +
    print( "\n\nExample 3" )
 +
    sum = 0
 +
    for i in range( 6 ):
 +
        try:
 +
            sum = sum + L[i]
 +
        except IndexError:
 +
            break
 +
        except TypeError:
 +
            pass
 +
    print( "sum = ", sum )
 +
   
 +
def main():
 +
    #example1()
 +
    L = [ 10, 3, 5, 6, 9, 3 ]
 +
    #example2( L )
 +
    #example2( [ 10, 3, 5, 6, "NA", 3 ] )
 +
    example3( [ 10, 3, 5, 6 ] )
 +
    example3( [ "NA", 10, 3, 5, 6 ] )
 +
   
 +
main()
 +
 +
</source>
 +
</onlydft>
 
<br />
 
<br />
 
<br />
 
<br />
 
[[Category:CSC111]][[Category:Exercises]][[Category:Python]]
 
[[Category:CSC111]][[Category:Exercises]][[Category:Python]]

Revision as of 10:29, 8 November 2011

Code in need of try/except statements

  • The program below is not very robust. We can easily make it crash.
  • Observe each function and see how to make it fail, or see why it will fail the way it is called
  • Make the program crash. Register the XXXXError that is generated. For example, if the output of the crash looks like this:

Traceback (most recent call last):
 File "/Users/thiebaut/Desktop/except0.py", line 29, in <module>
   main()
 File "/Users/thiebaut/Desktop/except0.py", line 27, in main
   example3( [ 10, 3, 5, 6 ] )
 File "/Users/thiebaut/Desktop/except0.py", line 18, in example3
   sum = sum + L[i]
IndexError: list index out of range

what you are interested in is IndexError. This is the exception you want to guard your code against.

  try:
      ........
      ........
  except IndexError:
      .........

  • Verify that you have made your functions more robust to erroneous input/data.

Code


def example1():
    x = eval( input( "enter a number: " ) )
    y = eval( input( "enter another number: " ) )
    print( x, '/', y, '=', x/y )

def example2( L ):
    print( "\n\nExample 2" )
    sum = 0
    for i in range( len( L ) ):
        sum = sum + L[i]

    print( "sum = ", sum )

def example3( L ):
    print( "\n\nExample 3" )
    sum = 0
    for i in range( 6 ):
        sum = sum + L[i]

    print( "sum = ", sum )
    
def main():
    example1()
    L = [ 10, 3, 5, 6, 9, 3 ]
    example2( L )
    example2( [ 10, 3, 5, 6, "NA", 3 ] )
    example3( [ 10, 3, 5, 6 ] )
    
main()

...