tynation.com

Monday, May 16, 2011

IT104

Notes


More on functions this week



def myfunction(var1, var2):
     total = var1 + var2
     print total
myfunction(10,10)

This is a bad way to quote because it's not as usable



def myfunction(var1, var2):
     total = var1 + var2
     return total
print "Your total is", myfunction(10,10)

The function call now holds the value here and can be reused for different values (potentially). Whatever is returned is the value of the function. You can also do math on the function call. print "Your total is", myfunction(10,10) + 50. You can also use a function as an argument for another function.



def myfunction(var1, var2):
     total = var1 + var2
     return total
def mul2(num)
     total = num * 2
     return total
print "Your total is", mul2(myfunction(10,10))

This can be quite powerful.


Lab Assignments


Purpose: Create a program that finds the distance an object will have fallen based on time


def fallingDistance(time):
     distance = 4.8 * time ** 2
     return distance
seconds = 1
while (seconds <= 10):
     if (seconds == 1):
          print "In",seconds,"second the distance fallen will be",fallingDistance(seconds),"meters"
     if (seconds != 1):
          print "In",seconds,"seconds the distance fallen will be",fallingDistance(seconds),"meters"
     seconds = seconds + 1

This program was a straight printing program. No inputs. No fun.


Purpose: Create a program to determine an objects kinetic energy based on user inputs for mass and velocity


def kineticEnergy (mass,velocity):
     kinE = .5 * mass * velocity ** 2
     return kinE
objMass = 0
objVel = 0
print "What is the mass of this object?"
objMass = input()
print "What is the velocity of this object?"
objVel = input()
print "Your object has",kineticEnergy(objMass,objVel),"kinetic energy"

I'm not sure what units kinetic energy is measured in. but kilogram meters squared per seconds to the fourth is a pretty nasty measurement. (I could be mistaken on this)


Homework


Purpose: Create a program that accepts scores for grades and displays their letter grade.


def calcAverage (grade,grades):
     avg = grade / grades
     return avg

def determineGrade(grade):
     if (grade < 60):
          return "F"
     elif (grade < 70):
          return "D"
     elif (grade < 80):
          return "C"
     elif (grade < 90):
          return "B"
     else:
          return "A"

numGrades = 0
cuGrade = 0
graded = 1
newGrade = 0
print "How many grades are there?"
numGrades = input()
while (graded <= numGrades):
     print "Enter grade for test",graded
     newGrade = input()
     cuGrade = cuGrade + newGrade
     print "Test",graded,"earned an",determineGrade(newGrade)
     graded = graded + 1
print "Average grade is",determineGrade(calcAverage(cuGrade,numGrades))

At first I was sticking to 5 inputs for grades. But Mr. Walker wanted me to do a cumulative total for the grade, so I thought about it for a bit and this came up with this.


Extra Credit


I shall post this on May 23


That's all for now


-Ty

No comments: