logo
Published on Models of Motion (http://www2.evergreen.edu/modelsofmotion)

Lab 3

1.  if overview

2.  else, elif

2+  Mastermind

3.  while overview

4.  Grades and average:

score=1
count=sum=0
while score!=0:
    score = input("enter score")
    if score<60:
        print("you failed")
    elif score<70:
        print("you have a D")
    elif score<80:
        print("you have a C")
    elif score<90:
        print("you have a B")
    else:
        print("you have an A")
    sum = sum + score
    count = count + 1

print "your average score is",sum/count

5.  Sentinel Value

6.  Random numbers

7.  Craps

VISUAL PYTHON

1.  Graph:  Integral

from visual.graph import *
from visual.factorial import factorial
 
xmax = input("enter domain range")
xmin = -xmax
 
myfunction = raw_input("enter function ")

def f(x): return eval(myfunction)
 
dx = (xmax-xmin)/512.0 # chop domain into 512 deltas
graph = gdisplay(ymin=xmin, ymax=xmax,xtitle=myfunction)
 
curve = gcurve(color=color.yellow)

area=0.0
#draw the function itself
for x in arange(xmin,xmax,dx):
    curve.plot(pos=(x,f(x)))
 
#draw the area under the curve between 0 and 2:  add area of each bar
funct2 = gvbars(color=color.red,delta=dx)
rate(2)
for x in arange(0,2,dx):
    funct2.plot(pos=(x,f(x)))
    area=area+dx*f(x)

print area

2.  Projectile

from visual import *
scene.width=800
scene.autoscale=0
ball = sphere(pos=(-9,-4.5,0), color=color.blue,radius=.3)

ang = input("enter angle")
ang = math.pi/180.0*ang
mag = input("input the force")
ball.velocity = vector(cos(ang)*mag, sin(ang)*mag,0)


d=.01
while ball.pos.y>=-4.5:
    rate(50)
    print ball.pos.x,ball.pos.y,ball.pos.z
    ball.velocity.y = ball.velocity.y-9.8*d
  
    ball.pos = ball.pos + ball.velocity*d

3.  Car

from visual import *
from visual.graph import *
time=0
scene.width=800
scene.height=100
scene.autoscale=0
graph = gdisplay(ymin=0 ,xmax=100,ymax =20,xmin=0,title="distance",height=300)
curve = gcurve(gdisply=graph,color=color.yellow)
graph2 = gdisplay(ymin=0 ,xmax=100,ymax =20,xmin=0,height=200,title="Acceleration")
curve2 = gcurve(gdisplay=graph2,color=color.cyan)
car = box(pos=(-10,0,0), color=color.red,height=.2,length=.5)
car.velocity = vector(0,0,0)


d=.01
print "rearrange windows then click on the car window to start"
scene.mouse.getclick()
while car.pos.x<10:
    rate(50)
    print car.pos.x,car.pos.y,car.pos.z
    if scene.kb.keys:
        s = scene.kb.getkey()
        if s=='s': car.velocity.x=car.velocity.x+.2
        if s=='a': car.velocity.x=car.velocity.x-.2
    car.pos = car.pos + car.velocity*d
    time=time+d
    print "time is",time
    curve.plot(pos =(time*10,car.pos.x+10))
    curve2.plot(pos =(time*10,3))

4.  Assignment

code = "rbygmc"
count=stop = 0


for a in code:
    for b in code:
        for c in code:
            for d in code:
                count = count + 1
                print "My",count,"guess is",a,b,c,d
               
                feedback = raw_input("enter the result in 1's and 0's")
                if feedback == "1111":
                    print "right"
                    stop = 1
                    break
            if stop==1:break
           
        if stop==1:break
    if stop==1:break
               

print "total guesses was",count
 


Source URL:
http://www2.evergreen.edu/modelsofmotion/modelsofmotion/lab-3