Skip to content

Oct 11: Using Visual Studio Code

Learning Objectives

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

  • Use VS Code to run, debug, and auto-format Python programs.
  • Explain the purpose of the idiom if __name__ == "__main__".
  • Describe several built-in modules, such as random and turtle.

Announcements

  • Mid-Eval Canvas survey – TODAY
  • Ch 8 on zyBooks – due Tuesday
    • Only 5 sections this week!
  • PA 1 FlexDoko (modules + testing)
    • Part A due Tuesday 10/22 (30 pts)
    • Part B due Tuesday 10/29 (70 pts)

VS Code Tutorial

[30 min]

  • Visual Studio Code Setup
    • If you are absent today, complete this tutorial on your own
    • Bring your completed VS Code setup to class next Monday

Lecture / Demo

[20 min]

Part 1

  • Value of the built-in variable __name__
    • When running move.py
    • When running stop.py
  • Purpose of if __name__ == "__main__":

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())

Part 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)

Exercise

For each outcome, describe the type of edit(s) necessary to draw.py and move.py:

  1. A blue turtle
  2. A longer simulation
  3. A smaller range of angles (e.g., -45 to 45) that define the direction of the turtle
  4. A random range of integers (e.g., 10 to 20) that define the length of a turtle move
  5. Produce the same outcome as #4 using forward(move.length()) instead of 10.