[SOLVED] I can't get the output I want from the function. The output I get is NoneType

Issue

The essence of the code is as follows – in the input, we have two parameters (product and weight), in the output we must get equivalent values: "Proteins", "Fats", "Carbohydrates" and "Calories" relatives to the weight of the product. In addition to the function, we calculate the sum of values ("Proteins", "Fats", "Carbohydrates" and "Calories"). I get NoneType when I output the values "Proteins", "Fats", "Carbohydrates" and "Calories" from the function when I want to get a list. I tried to declare the list "output" and add values "Proteins", "Fats", "Carbohydrates", and "Calories" to it, but I also get NoneType in the output.
Here is a code:

totalProteins, totalFat, totalCarbohydrates, totalCalories = 0.0, 0.0, 0.0, 0.0
listData = {}


def dietCalculator(product, weight):
    if weight == "info":
        if product in listData:
            return listData[product]
        else:
            print("No info")
            data = input("Type values: ").split(" ")
            listData[product] = float(data[0]), float(data[1]), float(data[2]), float(data[3])
            print(listData[product])
    elif weight == "add":
        data = input("Type values: ").split(" ")
        listData[product] = float(data[0]), float(data[1]), float(data[2]), float(data[3])
        print(listData[product])
    else:
        proteins = listData[product][0] * int(weight) / 100
        fats = listData[product][1] * int(weight) / 100
        carbohydrates = listData[product][2] * int(weight) / 100
        calories = listData[product][3] * int(weight) / 100
        return [proteins, fats, carbohydrates, calories]


while True:
    if input("Continue? ") != "No":
        inputs = input("Type product: ").split(" ")
        values = dietCalculator(inputs[0], inputs[1])
        totalProteins += values[0]
        totalFat += values[1]
        totalCarbohydrates += values[2]
        totalCalories += values[3]
    else:
        print("Total values for your meal: Proteins ", totalProteins, "Fat ", totalFat, "Carbohydrates ", totalCarbohydrates, "Calories ", totalCalories)
        break

Here is the error:

totalProteins += values[0]
TypeError: ‘NoneType’ object is not subscriptable

When I said, that i tried to declare the list "output" I changed:
return [proteins, fats, carbohydrates, calories]
to this:output = [] output.extend((proteins, fats, carbohydrateds, calories)) return output
And that didnt solve my problem

Solution

The problem was in while True branches. When the program is running and our input is, for example egg add our function, called dietCalculator, asks us to input 4 values for it

elif weight == "add":
        data = input("Type values: ").split(" ")
        listData[product] = float(data[0]), float(data[1]), float(data[2]), float(data[3])
        print(listData[product])

, to add this into dict. And at this point nothing came out of these brackets, there wasn’t any return. Thereby there wasn’t any inference from function, so, that`s why

values = dietCalculator(inputs[0], inputs[1])
totalProteins += values[0]
totalFat += values[1]
totalCarbohydrates += values[2]
totalCalories += values[3]

Line totalProteins += values[0] couldn’t work, because there is no data to work with (no inference from function, as I said)

Attaching working code

totalProteins, totalFat, totalCarbohydrates, totalCalories = 0.0, 0.0, 0.0, 0.0
listData = {}


def dietCalculator(product, weight):
    if weight == "info":
        if product in listData:
            return listData[product]
        else:
            print("No info")
            data = input("Type values: ").split(" ")
            listData[product] = float(data[0]), float(data[1]), float(data[2]), float(data[3])
            print(listData[product])
    elif weight == "add":
        data = input("Type values: ").split(" ")
        listData[product] = float(data[0]), float(data[1]), float(data[2]), float(data[3])
        print("Now values for", product, "are: Protein", listData[product][0], "Fats", listData[product][1], "Carbohydrates", listData[product][2], "Calories", listData[product][3])
    else:
        proteins = listData[product][0] * int(weight) / 100
        fats = listData[product][1] * int(weight) / 100
        carbohydrates = listData[product][2] * int(weight) / 100
        calories = listData[product][3] * int(weight) / 100
        return [proteins, fats, carbohydrates, calories]


while True:
    if input("Continue? ") != "No":
        inputs = input("Type product: ").split(" ")
        if not inputs[1].isdigit():
            print(dietCalculator(inputs[0], inputs[1]))
        else:
            values = dietCalculator(inputs[0], inputs[1])
            print("Values for this product are: Protein", values[0], "Fats", values[1], "Carbohydrates", values[2], "Calories", values[3])
            totalProteins += values[0]
            totalFat += values[1]
            totalCarbohydrates += values[2]
            totalCalories += values[3]
    else:
        print("Total values for your meal: Proteins ", totalProteins, "Fat ", totalFat, "Carbohydrates ", totalCarbohydrates, "Calories ", totalCalories)
        break

Answered By – roBoot

Answer Checked By – Marie Seifert (BugsFixing Admin)

Leave a Reply

Your email address will not be published. Required fields are marked *