DAY 8 Test 1 is Monday! == another string function == def ask_and_match(): '''Prompt the user to enter two strings, find out how many characters they have in common, and return the count as an int.''' word1 = raw_input("Enter a string: ") word2 = raw_input("Enter another string: ") common_count = count_matches(word1, word2) return common_count == introduction to while loops == - The function ask_and_match only gets one set of input from the user. - We'd like the user to continue entering strings until the two string provided have at least one character in common. - Other similar scenarios: type your password twice; must retry until get it to match! type your command; must retry until get a correct one - That means we'd like to repeat lines as long as common_count is 0 word1 = raw_input("Enter a string: ") word2 = raw_input("Enter another string: ") common_count = count_matches(word1, word2) - QU: How have we seen to make a chunk of code happen several times? For loops, like for char in s for pixel in pic - QU: How would you write a for-loop for this? Can't; would require us to have some specific object we are iterating over. Here, we are not going through some object, and have no idea the # of itns. - There's another kind of loop for our situation: a while loop while (condition): while-body - Let's write a new version of the function that uses while: [CODE: ask_and_match.py] def ask_and_match_repeat(): '''Prompt the user to enter two strings and find out how many characters they have in common. Repeat this task until the count is greater than 0, and return the count as an int.''' common_count = 0 # need an initial value for common count while common_count == 0: word1 = raw_input("Enter a string: ") word2 = raw_input("Enter another string: ") common_count = count_matches(word1, word2) return common_count - Debugger: run this multiple times and point out that the number of times the loop executes. - QU: Min number of times? 1 QU: Max number of times? infinite - How while loops are different from for loops - QU: Do they have loop variables? for - yes while loop - no, no specific loop variable - QU: When do they stop? for loop - definite - len of the sequence Can you predict the number of times a while loop will execute? nope! depends on when the condition becomes false. == Lists == - Why can we perform a for loop over a string? A string is a collection of data -- a collection of characters. - We often want collections, because we often want to do something to or calculate a result that relies on every item in a collection. (Sounds like a 'for' loop!) measurement1 = 45.27 measurement2 = 45.26 measurement3 = 45.24 ... - If we do ten measurements, we don't want ten variables. We want one variable that contains all ten measurements - You can do this in Python with a list measurements = [45.27, 45.26, 45.24] measurements measurements[0] measurements[1] measurements[2] measurements[3] # this results in an error # examine measurements in the debugger click on the triangle - Lists can contain more than just numbers, and even be heterogeneous. instructors = ["Andrew", "Jennifer", "Michael"] instructors student = ["Jon Reed", "University of Pittsburgh", 812391236, 3.45] - Lists are mutable. instructors[2] = "Jen" instructors instructors.append("Paul") instructors - QU: What kind of thing is "append(...)"? Yay, methods! == List functions == - Lists come with some useful functions - len: len(instructors) instructors[len(instructors) - 1] # Last item is at position len - 1 # Note: brackets can contain expressions - min, max, and sum: measurements max(measurements) # What do min and max do? min(measurements) sum(measurements) min(instructors) # What if the list is not a list of numbers? max(instructors) # min and max work as long as <, >, = are defined. sum(instructors) max("Hi, class!") # Works for strings, too. Comparing characters. min("Hi, class!") sum("Hi, class!") # But sum doesn't. == List Methods == dir(list) # List methods for lists help(list.append) # Note - does not say returns!!! measurements.append(45.27) measurements help(list.insert) measurements.insert(2, 44.99) measurements measurements.insert(len(measurements) - 1, 44.02) measurements help(list.sort) measurements.sort() == for loop over a list == - The pattern is for item in list: - Example: # Print the items in the list "measurements" for datum in measurements: print datum == Writing some code with lists == [CODE: first_list_code.py] - suggestion: Make these 2-minute exercises, in pairs. '''Print the length of each string in the list "instructors" ''' for s in instructors: print len(s) - Remember Design-Code-Verify again. QU: Did you do Design before Code? QU: Did you do Verify at all? QU: What are some "interesting" test cases? NEXT FUNCTION: def make_list_uppercase(original_list): '''Return a list of strings that contains the words from list 'original_list' but in all uppercase letters. Eg: if original_list is ['Hello', 'My Friendly', 'Kitten'] returned list is ['HELLO', 'MY FRIENDLY','KITTEN'] # Make a new, empty list as an accumulator uppercase_list = [] for s in original_list: uppercase_list.append(s.upper()) return uppercase_list NEXT FUNCTION def square_list(int_list): '''Return a list that contains the ints from list int_list squared.''' squared_list = [] for i in int_list: squared_list.append(i**2) return squared_list - Spend some time tracing through these in the debugger. Point out the format that a list is displayed in the debugger.