Search code examples
pythonnameerror

Why is my return function not allowing me to call the code


I was creating an if-else loop based on the type of variable, to either convert a list of numbers to kilograms, or simply one number, and for some reason I cannot call the variable I created into my main() function. I am a beginner to python and any help would be appreciated. Here is my code:

# Testing Code

def kgToLb(weight):
    # Return the converted weight (kg)
    newWeight = []
    if type(weight) == list:
        for w in range(len(weight)):
            return newWeight.append(weight[w] * 2.20462)
    return newWeight == weight * 2.20462



def main():
    weightList = [-22, 11, 0, 8.2, -8.2]
    answerKgsList = [-9.979044, 4.989522, 0, 3.71946186, -3.71946186]
    # Test data

    for w in range(0, len(weightList)):
        kgToLb(weightList)
        correctWeight = weightList == answerKgsList[w]
        print(correctWeight)
        print(newWeight)
        print("The converted weight is " + str(newWeight[w]) + ". " + str(correctWeight))


main()

I tried to change the if-else format to see if it would change anything to no avail.


Solution

  • You're very close! Here's a working solution:

    def kgToLb(weight):
        # Return the converted weight (kg)
        newWeight = []
        if type(weight) == list:
            for w in range(len(weight)):
                newWeight.append(weight[w] / 2.20462) # fix "return" and that you multiply. Should divide
        return newWeight # just return the new list
    
    def main():
        weightList = [-22, 11, 0, 8.2, -8.2]
        answerKgsList = [-9.979044, 4.989522, 0, 3.71946186, -3.71946186]
        # Test data
    
        newWeight = kgToLb(weightList) # save return of function to variable
    
        tolerance = 0.001 
        # when comparing two floats, always use some tolerance for when 
        # to consider them "equal.". If abs(A-B) < tol, then they're equal
    
        for w in range(0, len(weightList)):
            correctWeight = abs(newWeight[w] - answerKgsList[w]) < tolerance
            print("The converted weight is " + str(newWeight[w]) + ". " + str(correctWeight))
    
    main()
    # >>> The converted weight is -9.979044007584074. True
    # >>> The converted weight is 4.989522003792037. True
    # >>> The converted weight is 0.0. True
    # >>> The converted weight is 3.7194618573722456. True
    # >>> The converted weight is -3.7194618573722456. True
    

    Mainly the issue was that you used the return argument when you did the append() operation. This operation returns None so your function gets a bit messed up there. You were also multiplying by the conversion value instead of dividing. Also, when you're comparing two float values in python, you need a "tolerance" level for when you consider the two values equal. I.e. 0.030000001 will not be equal to 0.03 in python. I set your tolerance to 0.001 but you might want to change this depending on your level of precision. Have fun!