CMSC 201

Lab 9: Debugging

Welcome to lab!

Today we will practice debugging code! You will have to debug the three functions assigned to you for homework 5 (not the functions you wrote! We're giving almost working code to you.) The functions are provided as 3 separate files. Work on the first file, then after about 10 minutes whether you figured it out or not your TA will show you how to debug the code! This may seem easy but debugging is one of the most important skills you can learn, so pay attention!


BUT FIRST! A quick note on errors

There are two kinds of errors you will deal with in this lab:

  • Syntax errors, aka WHY WON'T MY CODE RUN!?! These happen when your code isn't formatted correctly, like missing a ")" or a ":", or your indentation is incorrect

  • Logic errors, aka WHY IS MY CODE NOT DOING THE THING?!? These happen when all the code in your program is grammatically correct, but the logic is messed up so it doesn't do what you are trying to make it do.

  • Sample Output:

    Should print: [1, 2, 3, 4, 5]
    Prints: []
    

    Final instructions

    Start with the letterCount function. This should have mostly syntax errors.
    Then do the sortList function. That one has two functions to debug with syntax AND logic errors so read the code carefully!
    Last, do the isPalindrome function. This has a big logic error so make sure you understand the code!

    def letterCount(letter):
        for i in myString:
            if i = letter:
                count+=1
        print(count)
    
    def main():
        print("Should print: 3\nPrints: ",end="")
        letterCount("a","aardvark")
    main()
    
    -----------------------------------------------------------------------------
    
    #Helper function for sortList
    def insertNum(number,myList,oldList):
    
        if len(myList)==0:
            myList.append(number)
        else:
            for i in range(len(myList)):
                if myList[i]> number and (number not in myList):
                    myList.insert(i,number)
                elif myList[i]< number and ( number not in myList):
                    myList.insert(i,number)
    
        return(myList)
    #sorts list
    def sortList(myList):
        newList = []
        myList=[]
        for i in (myList):
            newList = insertNum(i,newList,myList)
    
        print(newList)
    
    #test functions
    def main():
        print("Should print: [1,2,3,4,5]\nPrints: ",end="")
        sortList([4,5,3,2,1])
    main()
    
    -----------------------------------------------------------------------------
    
    #tests wheter a word is a palindrome
    def isPalindrome(myString):
        tempString = myString
        length = len(tempString)
        half = length/2
        isPalindrome=True
        for i in range(int(half):
            if myString[i] != myString[length-i]:
                isPalindrome = False
    
        print(isPalindrome)
    
    
    
    def main():
        print("Should print: True\nPrints: ",end="")
        isPalindrome("tacocat")
    
    main()