Reputation: 16479
I'm new to python so I'm trying to practice by making a simple macro-nutrient calculator. I'm not quite sure how to pass certain values to different functions.
Here is my code
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()
What I want to do is output a Dictionary (Macro_dict),
So I can print it out if the sum of all macros (Macro_sum
) is 100.
But I also want to check if Macro_sum
equals 100.
This means I have to output a value Macro_sum
into the function Total_macro_check
.
However I feel as though if my Macro_input
function returned Macro_sum
and Macro_dict
,
I cannot use its output in Total_macro_check
due to it returning more than one value, while Total_macro_check
only accepts 1 value.
Upvotes: 0
Views: 137
Reputation: 4374
Your function Macro_Input is bogey. I don't think this line behave as you think it does.
float(Percent_protein)
This is casting Percent_protein to float yes, but it's not assigning that value to anything.
Your func should be.
def Macro_input():
Percent_protein = float(input("Percentage of Protein: "))
Percent_carb = float(input("Percentage of Carbohydrates: "))
Percent_fat = float(input("Percentage of Fats: "))
Macro_dict = {'Protein': Percent_protein, 'Carbohydrate': Percent_carb, 'Fats': Percent_fat}
Macro_sum = Percent_protein + Percent_carb + Percent_fat
return (Macro_dict,Macro_sum)
Now it will return the dictionary and sum as you want. You can get them out with.
(dict,sum) = Macro_input()
Upvotes: 4
Reputation: 328770
Create a class to contain the values plus helper functions:
class MacroInput(object):
def __init__(self, protein, carb, fat):
self.protein, self.carb, self.fat = protein, carb, fat
def sum(self):
return self.protein + self.carb + self.fat
You can create an instance of this in Macro_input()
and then pass that instance around. def Total_macro_check(Macro_sum)
would then become:
def Total_macro_check(input):
sum = input.sum()
if sum == 100:
print "You macronutrients percentages are \n Protein:", input.protein, "%" #list for P/C/F --> %d
...
Upvotes: 1
Reputation: 3695
Modify Macro_input()
to return a tuple of Macro_dict
and Macro_sum
def Macro_input():
# your code
return Macro_sum, Macro_dict
Then you in your main function you can unpack the returned tuple into two variables like this:
def main():
print "Please enter your macro-nutrients"
Total_macro_value, Macro_dict = Macro_input()
Total_macro_check(Total_macro_value)
Upvotes: 2