0

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.

0

3 Answers 3

4

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()
Sign up to request clarification or add additional context in comments.

3 Comments

It doesn't answer the question title, but it answers what his problem actually is with his code in the question.
ahyeah I see you're right now actually. I just assumed (as asses do) from seeing that problem and the very end of the question that he was wanting to output each individual sum as he wasn't getting it to return what he wanted.
@KillianDS Should be fixed now to answer the ACTUAL question.
2

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)

Comments

1

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
    ...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.