I can't figure out a way to return a false boolean in this function. The function: nestedListContains(NL, target): takes a nested list of integers (NL) and an integer(target) and returns true if target is in NL. I can't get it to return false when the target integer is not in any part of the list. So nestedListContains([1,2,3,4],3) = true, but nestedListContains([1,2,3,4],5) would be false.
Everything I've tried returns a false statement before the entire nested list has been checked, so it returns false when actually true.
For example, nestedListContains([[9, 4, 5], [3, 8]], 3) is returning false but nestedListContains([2, 3, 5, 7, 9], 7) will actually work properly and return True.
I'm not sure how to think about this. Can you share some wisdom please?
BTW, this is an exercise, so I'm running this in an interpreter embedded in a website that gives programmed errors.
- Code: Select all
def nestedListContains(NL, target):
if NL == target:
for i in range(0,len(NL)):
Program executed without crashing.
The grader said:
Running nestedListContains([2, 3, 5, 7, 9], 7) … its value True is correct!
Running nestedListContains([2, 3, 5, 7, 9], 8) … its value False is correct!
Running nestedListContains([[9, 4, 5], [3, 8]], 3) … Error: nestedListContains([[9, 4, 5], [3, 8]], 3) has wrong value False, expected True