Lecture 10 (LAB03)

posted Feb 13, 2013, 5:59 AM by Samuel Konstantinovich   [ updated Feb 13, 2013, 7:41 AM ]

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

Python Lab (conditionals and 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:

to foo [a b]

    print a + b

end


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.

def FUNCTION_NAME ( PARAMETERS, GO , HERE) :

    TABBED CODE HERE

    TABBED CODE HERE

    TABBED CODE HERE

#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"


LAB03:


3.0. Make a header on your lab just like from Homework 01, it should be a comment. You can use # or ''' it doesn't matter.


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

Example:

triangleArea(5,10) should print 25


3.2. Write a function distance(x1,y1,x2,y2) which will print the distance between the points (x1,y1) (x2,y2).

Examples:

distance(0,0,3,4) prints 5

distance(1,-2,10,38) prints 41


3.3. 

Write a function checkDisc(a,b,c) that calculates the discriminant, and prints one of the following:

Equal Real Roots when the discriminant is 0

Real Roots when the discriminant is positive

Imaginary Roots when the discriminant is negative.

(Remember the discriminant of a quadratic equation is b^2 - 4ac )

3.4.

Write a function gradePicker(score) that accepts an integer score. The function prints 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


Examples

gradePicker(5) prints F

gradePicker(92) prints A



EXTRAS! (Optional things that won't be on Friday's test, but we will learn soon)

3.5 Make an improved version of distance called dist

dist(x1,y1,x2,y2) doesn't PRINT the value, instead it should return the value.

If you do it correctly you can say:

print(dist(0,0,3,4)) -->5

or

print ( 1 + dist(0,0,3,4) )   --> 6


This enables you to use the function in other functions. You probably want to use the dist function to write the next part:

3.6 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 are equidistant,

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.


examples:

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

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

etc.

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


Comments