####### Lecture 10 ######## # Michelle Szucs # To download as a Python file, right-click the link # and select "Save link as..." # NOTE: THIS CODE WILL NOT RUN WITHOUT COMMENTING # THE INCORRECT FUNCTION def dalkfjljk(aList): """Checks to see if aList contains one or more integers. aList: list returns True if there's one or more integers is in the list, else False.""" for elem in range(len(aList)): ## For every element in the list if (type(aList[elem]) == int) == True: print True ## Otherwise return False elif: # otherwise print False """ PROBLEMS 1. The code needs to be indented to actually be in the function. 2. It needs return statements, not print statements. 3. The function needs a good name. 4. The elif statement if completely wrong - you can't have an elif without a conditional statement, but we don't need and else or elif here anyway - and it won't work without a preceeding if statement. 5. No == True! If you don't understand your code without seeing the == True, put it in a comment next to the line 6. Comments should preceede the line they describe. 7. Comments should be indented to the same level as the line they describe. 8. Comments should explain confusing code. "otherwise" is useless. 9. The for loop should look like this: for elem in aList: if type(elem) == int: """ # The right way def containsInt(aList): """Checks to see if aList contains one or more integers. returns True if one or more integers is in the list, else False.""" for elem in aList: if type(elem) == int: return True return False # Example of string concatenation (adding) cat = "Snowy" dog = "Rainy" print "My cat's name is " + cat + " and my dog's name is " + dog + "." # Example of showing mutability - a will change a = [1,2,3] b = a b.append(4) print a # Don't use a while loop in cases like this - # you should you an if statement if the loop # is only going to go through one iteration isDone = True while isDone: x = x + 5 break