227 2022 Lab01 Introduction To Computation
227 2022 Lab01 Introduction To Computation
Objectives
Throughout your undergraduate program and your career, you will use computers to calculate. To help prepare
you, this introductory physics course (and subsequent physics courses in coming terms) includes a number of com-
putational exercises to predict and display the motion of interacting objects in 3D. You will use a programming
environment called VPython, which consists of the widely-used programming language Python plus a 3D graphics
module called “visual” which makes it possible to do vector algebra and to visualize vector quantities in 3D. VPython
(http://vpython.org) is free and runs on Windows, MacOSX, and Linux. It’s possible to run these programs in
a web-browser at a website: glowscript.org. To write and execute programs directly on your computer, you use an
editor called “VIDLE” (pronounced vee-idle) which comes with VPython.
You should finish this activity in one lab period and turn your work in at the end.
Note that the first line says ”GlowScript 2.1 VPython”, this tells Glowscript to treat your program as a visual
python program. Don’t modify this line. This line will be needed in all VPython programs in glowscript. If
running a program in VIDLE, make sure you start with the following two lines, which must go at the beginning
of every VPython program:
1
Matter & Interactions 4e Univ. of Calgary PHYS 227 Lab 01 Intro to Computation
Both kinds of errors are similar to errors you may make when working pencil and paper problems using a calculator.
Errors made entering expressions into your calculator (pressing the 8 key as opposed to the 5 key) are syntax errors.
Using an inappropriate or incorrect equation, or trying to divide a scalar by a vector, is an error in math, physics or
logic. These errors can give meaningless answers, but the computer won’t warn you!
⇒ Create a new VPython program as lab01ex02YourLastNames (lab 01, exercise 02, and change the commented
line to reflect that as well, but keep your names in there!) This new program is to make sure that you won’t
change the program that you are submitting.
⇒ Watch the VPython instructional video VPython Instructional Videos: A. Debugging Syntax Errors
http://vpython.org/videoA.html which illustrates some common errors.
⇒ Try modifying your program (lab01ex02 not the one that you’ve submitted for a grade) to introduce some of the
common errors shown in the video.
2
Matter & Interactions 4e Univ. of Calgary PHYS 227 Lab 01 Intro to Computation
VPython automatically “zooms” the camera in or out so that all objects appear in the window. This behavior is
called “autoscaling.” Usually this is helpful, but occasionally we may want to turn off autoscaling.
Since VPython automatically moves the camera to try to keep all objects in the display window, it can handle any
consistent set of units. We will always use SI units in our physics programs.
5.2 Comments
Comment lines start with a # (pound sign). A comment line can be a note to yourself, such as:
Or a comment can be used to remove a line of code temporarily, without erasing it.
You can also put a comment at the end of a line: sphere() # it’s round.
⇒ Comment out all but one arrow in your program. For the remaining arrow:
⇒ Change something in the arrow’s code such that the arrow is half as long and points in the opposite direction,
with its tail remaining on the same sphere.
7 Print Command
⇒ Start a new line at the end of your program and type:1
print(object.attribute)
⇒ Replace object.attribute with the name of one of your 3D objects and one of the valid attributes associated with
that object. For example, if you want to print the position attribute of a sphere named ball, it would look like
this:
print(ball.pos)
⇒ Run the program, and look below the window. You should see the value printed there.
⇒ You can also print text in VPython.
print("Here is the position of ball:", ball.pos)
⇒ Have your program print the position and radius of the ball (color does something counterintuitive that does
make sense). Supply text to state what each of these attributes are.
1 Another example of change in Python is that before Python 3.0, you could say print ball.pos, but starting with Python 3.0 one
must say print(ball.pos). If you are using an earlier version of Python, it is a good idea to use parentheses anyway, because it doesn’t
hurt, and it works with later versions of Python.
3
Matter & Interactions 4e Univ. of Calgary PHYS 227 Lab 01 Intro to Computation
⇒ Save the challenge task you just completed as Lab01ex02YourLastNames to submit to your TA.
⇒ Optional: If you have the time, come up with a pretty pattern in your VPython program and show it off your
VPython skills. There are cylinders, boxes, helices, rings, pyramids and many more! Explore different colors
and positions then save this as Lab01ex03YourLastNames. If you have more than one that you like save them as
Lab01ex03A, Lab01ex03B, etc.
E-mail your TA: Lab01ex01 and Lab01ex02 and if you did them any optional programs.
You must e-mail your programs to the TAs before you leave the lab. Make sure that each program has the names of
every in the group and has a name corresponding to which lab and exercise it is. Also make sure that every member
of your group has access to the code.
The first line is needed because there are different versions of Python, which is continuously improved and upgraded2 .
Note that there are two underscores immediately before and after the word “future.”
The second line tells the program to use the 3D module (called “visual”). The asterisk means, “Add to Python all
of the features available in the visual module”.
An error message in the Shell window typically consists of four lines of red text. The information you need is in the
bottom line – read the bottom line first.
2022-07-14
2 The statement (from space underscore underscore future underscore underscore space division, print function) tells the Python
language to treat 1/2 as 0.5, and makes the new form of print statements work on older versions of Python. You don’t need this statement
if you are using Python 3.0 or later, but it doesn’t hurt, because it is simply ignored by later versions of Python.