Oct 11: VS Code and Modules
VS Code Tutorial¶¶
Modules¶
- Value of the built-in variable
__name__
- When running
move.py
- When running
stop.py
- When running
- Purpose of
if __name__ == "__main__":
move.py
¶
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
¶
import move
print("in stop: __name__ ==", __name__)
print("from module: angle ==", move.angle())
Using Python's built-in turtle module¶
draw.py
¶
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¶
Try each of the following changes:
- A blue turtle
- A longer simulation
- A smaller range of angles (e.g., -45 to 45) that define the direction of the turtle
- A random range of integers (e.g., 10 to 20) that define the length of a turtle move
- Produce the same outcome as #4 using
forward(move.length())
instead of10
.