Skip to content

Oct 13: Writing Multiple Modules

Learning Objectives

After today's class, you should be able to:

  • Explain the purpose of the idiom if __name__ == "__main__".
  • Describe several built-in modules, such as random and turtle.
  • Summarize course and school policies about academic honesty.

Announcements

  • Mid-Eval Canvas survey – TODAY
  • Ch 8 on zyBooks – due Tuesday
    • Only 5 sections this week!
  • PA 1 Poker Dice (modules + testing)
    • Don't put off until after fall break
    • Part A due Tuesday 10/24 (30 pts)
    • Part B due Tuesday 10/31 (70 pts)

POGIL Activity

Example Code

Model 1

move.py
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
import random

def angle():
    number = random.randint(-90, 90)
    return number

print("in move: __name__ ==", __name__)
print("will always execute: angle ==", angle())

if __name__ == "__main__":
    print("only if True: angle ==", angle())
stop.py
1
2
3
4
import move

print("in stop: __name__ ==", __name__)
print("from module: angle ==", move.angle())

Model 2

draw.py
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
import move
import turtle

def randomwalk(steps):
    turtle.shape("turtle")
    turtle.color("green")
    for i in range(steps):
        turtle.left(move.angle())
        turtle.forward(10)
    turtle.bye()

if __name__ == "__main__":
    randomwalk(100)