# 7.8 Help searching a TypeError

 0 Hello! Iam curious about Final 7.8 . When i hit run, it says everything is correct, but when submitting I get an TypeError and I cant figure out why. Maybe someone can point it out to me? Thanks in advance My procedure is: def is_list(p): return isinstance(p, list) def same_structure(a,b): if (not is_list(a) and not is_list(b) ): return True if (is_list(a) and is_list(b)): if (len(a) != len(b)): return False counter = 0 check = True; while check and counter < len(a)-1: if (not is_list(a[counter]) and not is_list(b[counter]) ): counter = counter + 1 if (is_list(a[counter]) != is_list(b[counter])): check = False if (is_list(a[counter]) and is_list(b[counter])): if (len(a) != len(b)): check = False else: check = same_structure(a[counter],b[counter] ) counter = counter +1 return check  asked 14 Apr '12, 12:56 kai 6●3 accept rate: 200% malckwan 4.7k●10●28●94

 2 Your code assumes that all elements within lists will be a list. A list may contain just numbers. So in a case where, a = [ [2, 3], 4, [3] ] and b = [ [3, 5], [5, 6], [4] ], your code will attempt to evaluate the length of the second element in a (which is an integer 4 and has no length). Your procedure needs a third conditional that checks if only one of the two inputs is a list. Something like : if is_list(a) != is_list(b): return False  link This answer is marked "community wiki". answered 14 Apr '12, 13:03 malckwan 4.7k●10●28●94 Thanks for the answer but I my Idea (eclipse, pydev) says it runs just fine. (Evaluate to false) if __name__ == '__main__': d = [ [2, 3], 4, [3] ] e = [ [3, 5], [5, 6], [4] ] print same_structure(d,e) print same_structure(e, d)  (14 Apr '12, 13:10) kai 1 Try this : print same_structure(1, [2, 1, 2])  Your code throws a TypeError. Also, as @cthompson says below, if d = [1] and e = 7, your code evaluates to True, which is clearly wrong. Sorry, the example in my answer above was an example I just made up but didn't test. If you compare a number and a list, at the top level, you'll get the TypeError. (14 Apr '12, 13:14) malckwan 1 You have made me realise that I shouldn't be lazy and I should write my own test cases. I just used ones I found on the forums and clearly they're not exhaustive enough. Thanks! (14 Apr '12, 13:18) Chris Thompson Stupid me. Thanks for the help! (14 Apr '12, 13:31) kai
 1 I tested your code on several test cases and none of them produced an error but one returned the wrong value. print same_structure([1], 7) returns True  For reference, these were the test cases I ran: print same_structure([ [2, 3], 4, [3] ] ,[ [3, 5], [5, 6], [4] ]) == False print same_structure(3, 7) == True print same_structure([1, 0, 1], [2, 1, 2]) == True print same_structure([1, [0], 1], [2, 5, 3]) == False print same_structure([1, [2, [3, [4, 5]]]], ['a', ['b', ['c', ['d', 'e']]]]) == True print same_structure([1, [2, [3, [4, 5]]]], ['a', ['b', ['c', ['de']]]]) == False print same_structure([],[]) == True print same_structure('','') == True print same_structure([],['']) == False print same_structure('',3) == True print same_structure(3,'audacity') == True print same_structure([1, [], 1], [2, [5], 3]) == False print same_structure([1,2,3,[1,[1],2], [[1,2]]], [1,2,3,[1,[],2], [[1,2]]]) == False print same_structure([1,2,3,[1,1,2], [[1,2]]], [1,2,3,[1,[],2], [[1,2]]]) == False print same_structure([1], 7) == False print same_structure([[1,2,3,['d']],4,[1,['g','g',[]]]], [['h',1,'f',[8]],'y',['k',[4,7,8]]]) == False print same_structure ([1,[1]],[1,[1,2,3]]) == False  answered 14 Apr '12, 13:11 Chris Thompson 2.3k●3●12●38 Ok, I should have made my own test cases. Thanks for the help! (14 Apr '12, 13:30) kai
Question text:

Markdown Basics

• *italic* or _italic_
• **bold** or __bold__
• image?![alt text](/path/img.jpg "Title")
• numbered list: 1. Foo 2. Bar
• to add a line break simply add two spaces to where you would like the new line to be.
• basic HTML tags are also supported

×15,320
×683
×172
×112
×35
×19