CMSC 201

Lab 6: Functions

Part 1

First, we are going to be fixing code that has syntax errors. Examples of a syntax error are when you miss a colon, or forget to close a parenthesis. Copy the code below, and try to run it:
def findNumInList():
for i in myList:
if(myList[i] = num)
print("Found number %s, num)

def main():
myList = [1,25,7,99,12]

//Gets number from user, and appends it to the existing list
num = int(input("Enter a number to be added to the end of the list")
myList.append(num)

//Checks to see if number was successfully added to the list
findNumInList()
main()
You'll notice the code gives you a syntax error. There are two approaches you can take to fixing this code:
  • Glance through the code and check to see if anything looks majorly wrong.
  • Fix the first error, fix the next error that pops up, etc.

Part 2

The next program has semantic errors. This means that the code runs, but it doesn't function in the way that we intended. Copy the code below, and try to run it:
#getSum calculates the sum of all values in the given list
#Input: gradesList, a list of grades
#Output: sumValue, the sum of all grades
def getSum(gradesList):
    sumValue = 0

    #loops over all values, incrementally adding each one
    for i in range(0,len(gradesList)-1):
        sumValue = sumValue + gradesList[i]

    return sumValue

#getMedian finds the median for a given list. Accounts for two cases:
# 1) If the list has an odd amount of numbers, the median is the middle number
# 2) If the list has an even amount of numbers, the median is the average
# of the two numbers closest to the middle of the list
#Input: gradesList, a list of grades
#Output: the median value of the input list
def getMedian(gradesList):
    sortedGrades = sorted(gradesList)

    #Case for if the amount of elements in the list is odd
    if len(sortedGrades) % 2 == 0:
        return sortedGrades[int((len(sortedGrades))/2)]
    #otherwise case for when the list is even
    else:
        lower = sortedGrades[int(len(sortedGrades)/2-1)]
        upper = sortedGrades[int(len(sortedGrades)/2)]

    return (float(lower + upper)) / 2

#getAverage calculates the average of a given list- uses
#getSum() to calculate the sum
#Input: gradesList, a list of grades
#Output: average, the average value of the input list
def getAverage(gradesList):
    sumValue = getSum(gradesList)

    average = sumValue/len(gradesList)

    return average

#getAverageIfPresent calculates the average of a given list, minus
#any values that are zero
#Input: gradesList, a list of grades
#Output: average, the average value of the input list
def getAverageIfPresent(gradesList):
    
    #loops over every grade, checking for a 0
    #if a 0 is found, it will be removed
    for grade in gradesList:
        if (grade == '0'):
            gradesList.remove(grade)

    sumValue = getSum(gradesList)
    average = sumValue/len(gradesList)

    return average

def main():
    #If a student was absent, they got a zero.
    grades = [0,90,52,75,66,88,0,100]

    #Calculates values
    average = getAverage(grades)
    median = getMedian(grades)
    averageOfPresent = getAverageIfPresent(grades) 

    print("The average of the grades is:",average)
    print("The average of the grades (minus any absent students) is:",averageOfPresent)
    print("The median of the grades is:",median)

main()

You'll notice that the code runs, but to figure out what type of output you should be getting read the function headers and comments. Here are some approaches you can take to solving this problem:

  • Play around with the grades list. With the values in the list now, it's difficult to know off the top of your head what the outputs should be. Shorten the list or change the values to easier values so that you are able to mentally verify the output.
  • Figure out what a function should be returning for a given input. Test that individual function by calling it in main. Print out what it is returning by printing the function call with your test input value(s).
  • Determine which functions are dependent on each other. For example, if function 1 is calling function 2, you can't know that function 1 is working until you prove that function 2 is working.