











Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!












CSSE 120—Rose Hulman Institute of Technology
Programs (scripts) Modules designed to run directly (not imported) Libraries Modules imported and designed not to run directly Hybrid Can do both Add to end of module: if name == 'main' main() Two underscores Great for testing!
Separates main code from code for special cases
Keeps the main code clean Makes the expected special cases clear Says ―do these steps and if an exception occurs, handle it this way‖
Example:
Download from Lessons Modules to Download … Session 10 slope.py Try with: (5, 6) and (10, 4) (3, 6) and (10, 12) (3, 300) and (3, 500)
Can use if statement to take care of this special case.
We can instead use exception handling: try statement try:
except :
Algorithm code goes in body of try clause
Exceptions are caught and handled in except clause.
Type of exception that could be generated
Like multi-way decisions with if-elif-else statements try:
except :
… except :
except:
Try (3, 300) and (a, 500)
Modify slope.py to include an except clause for a syntax error exception
Try (2, 100) and (4 100) missing comma!
Try
**>>> dir(builtins)
Typical debugger commands:
Set a breakpoint—place where the debugger will pause the program Single step—execute one line at a time Inspect a variable—look at its changing value over time
Debugging Example
Download printFactorial.py from Modules to Download Session 10
This is the Debug perspective
A all the variables view that shows
Ashows all the view that executingfunctions
This shows the line being view is an editor that executed and lets you makechanges to the file
Athe outline of the view that shows module beingexamined ( Outline View )
Review: For loop
Definite loop: knows a priori the number of iterations of loop body Counted loop: sequence can be generated by range() Example for loop in slideshow.py
Syntax:
for in :
#Open the file inputFile = open(inputFileName, 'r')
for line in inputFile: image = Image(imageCenter, line.rstrip()) image.draw(win) time.sleep(delay)
win.getMouse() inputFile.close() win.close()
A pre-test loop
Condition is tested at the top of the loop
Example use of while loops
Nadia deposits $100 in a savings account each month. Each month the account earns 0.25% interest on the previous balance. How many months will it take her to accumulate $10,000?
Download getFile.py from Modules to Download Session 10 Put file in same project that we've been using Run the module with various inputs, like: getFile.py slope.py notARealFileName
Let's fix it!
Hint: You'll need code like the fixed version of this for your homework!
See Homework 10 instructions on Angel: Lessons Homework Homework 10
Reading and ANGEL quiz due session 11
Problem 3:
Due session 12, but you'll have more homework assigned session 11 so start now! Pair Programming