我是 python 新手,所以我试图通过制作一个简单的宏量营养素计算器来练习。我不太确定如何将某些值传递给不同的函数。
这是我的代码
def Macro_input():
Percent_protein = input("Percentage of Protein: ")
float(Percent_protein)
Percent_carb = input("Percentage of Carbohydrates: ")
float(Percent_carb)
Percent_fat = input("Percentage of Fats: ")
float(Percent_fat)
Macro_dict = {'Protein': Percent_protein, 'Carbohydrate': Percent_carb, 'Fats': Percent_fat}
Macro_sum = Percent_protein + Percent_carb + Percent_fat
return Macro_sum
def Total_macro_check(Macro_sum):
#perhaps put all input into a dictionary? Macro['Protein':num, 'Carb':num, 'Fat':num]
if Macro_sum == 100:
print "You macronutrients percentages are \n Protein: "#, Macro_dict['Protein'], "%" #list for P/C/F --> %d
elif Macro_sum < 100:
print "Total percentages do not add up to 100. Please reenter percentages."
#go back to function that asks for Macros
elif Macro_sum > 100:
print "Total percentages surpass 100. Please reenter percentages."
#go back to function that asks for Macros
def main():
print "Please enter your macro-nutrients"
Total_macro_value = Macro_input()
Total_macro_check(Total_macro_value)
if __name__ == "__main__":
main()
我想要做的是输出一个字典(Macro_dict),所以如果所有宏( )
的总和是100,我可以打印出来。
但我也想检查是否等于100。
这意味着我必须输出一个值到功能。 Macro_sum
Macro_sum
Macro_sum
Total_macro_check
但是我觉得好像我的Macro_input
函数返回了Macro_sum
,
我不能使用它的输出,Macro_dict
因为它返回多个值,而只接受 1 个值。 Total_macro_check
Total_macro_check