OIM3640 - Problem Solving and Software Design

2026 Spring

Session 06 (2/5)

contain

Today's Agenda

Announcements

  • What you should have done:
    • Read and run Chapter 3 notebook
    • Worked on Chapter 3 exercises
    • Updated Learning Logs (logs/s05.md)
  • Keep pushing your work to GitHub regularly!
  • Questions?

What We've Learned So Far

  • Variables, expressions, statements
  • Types: int, float, str, bool, NoneType
  • Operators: +, -, *, /, //, %, **
  • Functions:
    • Syntax:
      def f(parameter):
          """ docstring """
          # function body
      
    • Variables (defined inside a function) and parameters are local.
    • Discussions on print() vs. return: 1, 2, 3, 4...

🙋 Quick Quiz

1️⃣ What are the types of x, y, z?

x = "42"
y = 42
z = 42.0

2️⃣ What does this print?

name = "Python"
print(name * 3)
print(name + "3")

🙋 Quick Quiz (continued)

3️⃣ What does this print?

def double(x):
    return x * 2

print(double(5))
print(double("Hi"))

4️⃣ What does this print?

a = 5
b = a
a = 10
print(b)

🙋 Quick Quiz (continued)

5️⃣ What does this print?

x = 10

def foo():
    message = "Hello"
    x = 
    return x

print(foo())
print(x)
print(message)

Chapter 4 - Functions and Interfaces

What we'll learn:

  • The turtle module for graphics
  • Interface design principles
  • Refactoring and generalization
  • Docstrings and documentation

The Turtle Module

Python's turtle module lets you draw graphics:

import turtle

t = turtle.Turtle()
t.forward(100)    # Move forward 100 pixels
t.left(90)        # Turn left 90 degrees
t.forward(100)

Great for learning loops, functions, and visual thinking!

Basic Turtle Commands

Command What it does
forward(d) Move forward d pixels
backward(d) Move backward d pixels
left(angle) Turn left by angle degrees
right(angle) Turn right by angle degrees
penup() Stop drawing
pendown() Start drawing

Drawing a Square

import turtle

t = turtle.Turtle()

t.forward(100)
t.left(90)
t.forward(100)
t.left(90)
t.forward(100)
t.left(90)
t.forward(100)

Notice the repetition? We can do better with a loop!

Drawing a Square with a Loop

import turtle

t = turtle.Turtle()

for i in range(4):
    t.forward(100)
    t.left(90)

Much cleaner! This is the power of loops.

Encapsulation: Making a Function

Let's encapsulate the square-drawing code:

def draw_square(t, length):
    """Draw a square with the given turtle and side length."""
    for i in range(4):
        t.forward(length)
        t.left(90)

Now we can reuse it:

draw_square(t, 100)
draw_square(t, 50)

Generalization: From Square to Polygon

What if we want to draw any polygon?

def draw_polygon(t, n, length):
    """Draw a polygon with n sides."""
    angle = 360 / n
    for i in range(n):
        t.forward(length)
        t.left(angle)
draw_polygon(t, 3, 100)   # Triangle
draw_polygon(t, 5, 80)    # Pentagon
draw_polygon(t, 6, 60)    # Hexagon

Interface Design

A good interface is:

  • Simple: Easy to understand and use
  • General: Works for many cases
  • Appropriate: Not too simple, not too complex
# Too specific
def draw_pentagon(t):
    ...

# Better: more general
def draw_polygon(t, n, length):
    ...

Refactoring

Refactoring = improving code structure without changing behavior

Common refactoring patterns:

  1. Extract function: Take repeated code and make it a function
  2. Generalize: Add parameters to make functions more flexible
  3. Rename: Use clearer names for variables and functions

Drawing a Circle

A circle is just a polygon with many sides:

def draw_circle(t, radius):
    """Draw a circle with the given radius."""
    import math
    circumference = 2 * math.pi * radius
    n = 50  # number of sides
    length = circumference / n
    draw_polygon(t, n, length)

We reuse draw_polygon — no need to write new loop logic!

The Development Plan

  1. Start simple: Write code that works for one case
  2. Encapsulate: Wrap it in a function
  3. Generalize: Add parameters for flexibility
  4. Refactor: Clean up and improve
  5. Document: Add docstrings

This iterative process is how real programmers work!

Docstrings Revisited

def draw_polygon(t, n, length):
    """
    Draw a regular polygon.

    Parameters:
        t: Turtle object
        n: Number of sides
        length: Length of each side in pixels
    """
    angle = 360 / n
    for i in range(n):
        t.forward(length)
        t.left(angle)

📝 Your Turn

Open Chapter 4 notebook (chap04.ipynb)

Work through the examples and exercises:

  • Draw different shapes with turtle
  • Create your own functions
  • Practice encapsulation and generalization

Questions? Ask now!

Before You Leave (5 min)

  • Any questions so far?
  • Start your Learning Log for today (logs/s06.md)
  • Work on Chapter 4 exercises
  • Note down questions for next time
  • Push your work to GitHub

Next session: Conditionals and Recursion (Chapter 5)

global styles