Problem 4 nested loop pattern (5 pts) Write a function printCommonLetters()that
ID: 672822 • Letter: P
Question
Problem 4 nested loop pattern (5 pts)
Write a function printCommonLetters()that has two input arguments: lst1 and lst2, which are two lists of strings. The function will print the items that are the same in both lists. When you are done iterating over both loops, print a final statement like ‘done’ or ‘goodbye’. We discussed in class how execution resumes with non-indented statement that is aligned with for in the for loop statement.
For example if lst1 = [ ‘ab’, ‘cd’, ‘ef’, ‘gh’] and lst2 = [‘abc’, ’geh’, ‘cd’, ‘ab’], the function will print: ‘ab’
‘cd’ ‘goodbye!’ in python 3.4
Explanation / Answer
def printCommonLetters (list1,list2):
for item in list1:
if item in list2:
print item + ' '
print 'goodbye!'
lst1 = ['ab', 'cd', 'ef', 'gh']
lst2 = ['abc', 'geh', 'cd', 'ab']
printCommonLetters(lst1,lst2)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.