2014-02-27 LAB04

posted Feb 26, 2014, 6:15 PM by Samuel Konstantinovich   [ updated Feb 27, 2014, 12:55 PM ]

Reminder: Test NEXT Friday (HTML, terminal commands, Python)

Do Now: (while you work on lab, update this in the background)

1. Go to: Codingbat.com and log in.

2. Go to prefs and "Share to:" konstans@stuycs.org

3. Also in prefs: change your name to:     PerX,_Last,_First    (Capitalize Per, and your names!)

Replace  X  with either 2 or 3

 _  is a space, do not write an underscore

Replace Last and First with your Last and first names


per3, Bach, Johan

Goal:  Python Lab (conditionals and functions)

Lab04 (5 functions)

Pre-lab notes:

You will use the if/elif/else statements we looked at yesterday. We will also look at simple function definitions:

Here is a basic netlogo function that does not return a value:

to foo [a b]

    print a + b


We can write it in python as follows:

def foo ( x, y) :

    print x + y

#this is outside the function

Notice Python functions do not require an ending statement because like if statements they contain all of the tabbed code. Once the tab ends, the function ends.





#end of function is when you stop tabbing

Also notice that parameters go in parenthesis separated by commas. If there are no parameters, you need an empty set of parenthesis:

def spam():

    print "This doesn't really do much"

This function return's a value, so you can say    calcSum(2,3) + 1    and the calcsum will evaluate to 5.

def calcSum(a,b):

    return a+b


3.0. Make a header on your lab it should be a comment with your name, period, and assignment name/number.

3.1 and 3.2 are warmups. You must check your functions on values you know the answer to. 

3.1. Write a function triangleArea that accepts two numbers: base and height, it should return the area


print triangleArea(5,10) #this prints 25

The reason we want the function to return a value is so we can:

print triangleArea(5,10)


x = triangleArea(5,10)  


Write a function distance(x1,y1,x2,y2) which will evaluate to the distance between the points (x1,y1) (x2,y2). The order of the parameters is important. x,y,x,y


print distance(0,0,3,4) #would print 5

x=distance(1,-2,10,38) #assigns x a value of 41, you can print x to check it.


Write a function disc(a,b,c) that calculates and returns the discriminant given the coefficients of a quadratic equation A, B, and C.  

(Remember the discriminant of a quadratic Ax^2+Bx+C = 0 is B^2 - 4AC )


disc(2,3,4)  evaluates to -23

disc(1,2,1)  evaluates to 0

#remember if you don't print a function it won't show up when you run your code. In the future I will say what something evaluates to, I don't have to remind you to print it. 


Write a function gradePicker(score) that accepts an integer score. The function evaluates to the letter grade according to the following:

90 or higher is 'A'

80-89 is 'B'

70-79 is 'C'

65-69 is 'D'

otherwise 'F'


gradePicker(5) evaluates to 'F'

gradePicker(92) evaluates to 'A'

3.5 Write a function closestPoints(ax,ay,bx,by,cx,cy)  that takes the coordinates of points a b and c. 

The function should return 0 when they all pairs of points are equidistant to eachother. NOTE: To get  0, the coordinates have to be irrational. 

return 1 when A and B are closest together,

return 2 when A and C are closest together,

return 3 when B and C are closest together.


closestPoints(0,0,1,1,5,5) --> 1    ( an arrow denotes 'evaluates to' )

EDIT: I had a bad example with a 0.

closestPoints(0,0,1,1,0.1,0.1) --> 2

closestPoints(-1,0,0,1,1,0,) --> 3


When two of them are equidistant to the same point, just take either of them

closestPoints(0,0,0,1,0,2)   #ab and bc are the same distance, take either one. 

that would return either 1 or 3 , either would be correct. 

Submiting your Lab

1-The lab SHOULD BE DONE by tonight. I will be due on the HW server Friday Night. You will get other classwork on Friday, the extra day is just in case you get stuck and need more time. 

2-Submit ONLY the function definitions. Do not print anything. I will run test cases myself on your code. You should thoroughly test to see that your functions work prior to submitting.