The return value should be a list but doesn't return as expected?! - Python newbie

Posted by user1432941 on Stack Overflow See other posts from Stack Overflow or by user1432941
Published on 2012-06-10T22:20:59Z Indexed on 2012/06/10 22:40 UTC
Read the original article Hit count: 154

Filed under:

Hi this must be a very simple solution that has eluded me this last hour. I've tried to build this test function where the return value of the test_cases list should match the values in the test_case_answers list but for some reason, test case 1 and test case 2 fail. When i print the return values for the test cases they return the correct answers, but for some reason test case 1 and test case 2 return False.

Thanks for your help!

import math

test_cases = [1, 9, -3]  
test_case_answers = [1, 3, 0]

def custom_sqrt(num):  
    for i in range(len(test_cases)):  
        if test_cases[i] >= 0:  
            return math.sqrt(test_cases[i])  
        else:  
        return 0

for i in range(len(test_cases)):  
    if custom_sqrt(test_cases[i]) != test_case_answers[i]:  
        print "Test Case #", i, "failed!"


custom_sqrt(test_cases)

© Stack Overflow or respective owner

Related posts about python