首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >我无法从函数中得到我想要的输出。我得到的输出是NoneType

我无法从函数中得到我想要的输出。我得到的输出是NoneType
EN

Stack Overflow用户
提问于 2022-02-04 17:34:12
回答 1查看 48关注 0票数 -1

代码的本质如下-在输入中,我们有两个参数(产品和重量),在输出中我们必须得到等价的值:“蛋白质”、“脂肪”、“碳水化合物”和“卡路里”与产品的重量相关。除了函数,我们计算值的总和(“蛋白质”,“脂肪”,“碳水化合物”和“卡路里”)。当我想要得到一个列表时,当我输出“蛋白质”、“脂肪”、“碳水化合物”和“卡路里”值时,我得到了NoneType。我试图声明列表中的“输出”,并添加值“蛋白质”、“脂肪”、“碳水化合物”和“卡路里”,但我在输出中也得到了NoneType。这是一个代码:

代码语言:javascript
复制
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

以下是错误:

totalProteins +=值TypeError:'NoneType‘对象不可订阅

当我说,我试图声明列表“输出”时,我将:return [proteins, fats, carbohydrates, calories]改为:output = [] output.extend((proteins, fats, carbohydrateds, calories)) return output,但这并没有解决我的问题。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-02-05 19:25:32

问题出在while True分支机构。例如,当程序运行并且我们的输入是egg add时,我们的函数dietCalculator要求我们为它输入4个值。

代码语言:javascript
复制
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])

,把这个加到丁字里去。在这一点上,没有从这些括号中出现,没有任何return。因此没有任何函数的推论,所以,这就是为什么

代码语言:javascript
复制
values = dietCalculator(inputs[0], inputs[1])
totalProteins += values[0]
totalFat += values[1]
totalCarbohydrates += values[2]
totalCalories += values[3]

totalProteins += values[0]无法工作,因为没有可处理的数据(正如我所说的,没有函数的推断)

附加工作代码

代码语言:javascript
复制
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
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70990539

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档