代码的本质如下-在输入中,我们有两个参数(产品和重量),在输出中我们必须得到等价的值:“蛋白质”、“脂肪”、“碳水化合物”和“卡路里”与产品的重量相关。除了函数,我们计算值的总和(“蛋白质”,“脂肪”,“碳水化合物”和“卡路里”)。当我想要得到一个列表时,当我输出“蛋白质”、“脂肪”、“碳水化合物”和“卡路里”值时,我得到了NoneType。我试图声明列表中的“输出”,并添加值“蛋白质”、“脂肪”、“碳水化合物”和“卡路里”,但我在输出中也得到了NoneType。这是一个代码:
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,但这并没有解决我的问题。
发布于 2022-02-05 19:25:32
问题出在while True分支机构。例如,当程序运行并且我们的输入是egg add时,我们的函数dietCalculator要求我们为它输入4个值。
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。因此没有任何函数的推论,所以,这就是为什么
values = dietCalculator(inputs[0], inputs[1])
totalProteins += values[0]
totalFat += values[1]
totalCarbohydrates += values[2]
totalCalories += values[3]行totalProteins += values[0]无法工作,因为没有可处理的数据(正如我所说的,没有函数的推断)
附加工作代码
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)
breakhttps://stackoverflow.com/questions/70990539
复制相似问题