Lab_0 - Lab 0 VPython Review Lab 0 VPython Review...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
Lab 0: VPython Review 6 Lab 0: VPython Review OBJECTIVES In this lab you will: Learn the basic structure of a computer program Create objects in VPython Display vector quantities in 3D Animate the motion of 3D objects Construct a graph in VPython In this lab we will review the use of VPython by using the concept of electric fields. From last semester, recall that VPython is a simple, programming language which can: Display objects Display vectors representing physical quantities Animate object motion Simulate physical interactions Create graphs Output results from calculations. 1) The Computer Program A computer program consists of a sequence of instructions. The computer carries out the instructions one by one, in the order in which they appear, and stops when it reaches the end. The syntax of the instructions must be exact. If the computer encounters an error in an instruction, it will stop running and print a red error message. A typical program has five sections: Setup statements Definitions of constants Setting initial conditions Creation of objects Calculations to predict motion or move objects (these may be repeated using loops ) 2) Opening and Setting Up VPython VPython is installed on all these lab computers. It is also on all ITAP computers. a) Open the Python editor, IDLE, through Start -> All Programs -> Course Software -> science -> phys -> Python 2.4 -> IDLE for VPython. This procedure may be different in you lab. Your TA will tell you if it is different. You can also open programs by right clicking on them and selecting “Edit with IDLE,” but this will cause your IDLE window to close if you close the display window, so avoid this method. b) Enter the following lines of code in the IDLE editor window.
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Lab 0: VPython Review 7 from __future__ import division from visual import * from visual.graph import * Every VPython program begins with the first two lines, the third line is optional. The first line tells the Python language to treat 1/2 as 0.5. Without the first statement, the Python programming language does integer division with truncation i.e. 1/2 is zero! The second line tells the program to use the 3D module (called “visual”), which allows us to easily draw many 3D objects. The third line allows VPython to output graphs. You will not need this line in all your programs, but there is no harm in including it. If you are using a graph (you will be in this first program), you may wish to include the following after the above three lines. scene = display(x=0, y=0, width=600, height = 600) graph = gdisplay(x=600, y=0, width=400, height=300) This positions the display and graph window so that they do not block each other. Now VPython is ready to receive instructions. 3) Commenting You can use comments to make your code easier to read. A comment is a statement that you can see on the screen but the computer will not interpret it as an instruction. VPython knows words are comments if they are preceded by a pound sign (#). The # may be at the start of a line in order to comment out the entire line, or it may be midline in order to comment out a description of what the line of code does.
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

Page1 / 7

Lab_0 - Lab 0 VPython Review Lab 0 VPython Review...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online