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

Question:

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.

Asked By: Santi

||

Answers:

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!

You are returning newWeight too soon. (Actually, you are incorrectly returning the None value that newWeight.append produces too soon.)

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)
        return newWeight
    return weight * 2.20462

There are a few other problems with this function, but it’s not worth going over them right now becauase this function is too complex to begin with. It should do one thing: convert a weight in kilograms to a weight in pounds.

def kgToLb(weight):
    return weight * 2.20462

A second function, if even needed, should use that to convert a list of weights.

def many_kgToLb(weights):
    return [kgToLb(w) for w in weights]

The caller knows if they have a single weight or a list of weights; let them pick the right function to call rather than making a single function make the decision.

Answered By: chepner
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.