HiveBrain v1.2.0
Get Started
← Back to all entries
patternpythonMinor

Calculate proper dosage of supplements to take

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
takedosagecalculatepropersupplements

Problem

Well, that's my first "program" after couple days of working with Python and programming itself. That may be stupid since everything there is so basic but I wanted to ask if there are any spaces to improve it. Idea was to give the user proper dosage of 4 supplements regarding if this is training day or not.

Is there any way to achieve it faster, more simple, better? Appreciate all feedback!
Edit

Thanks for all your replies! I will go through all of them and learn from them what i can, still confused in programming world.

dayon = {"BCAA" : "10g",
  "White" : "50g",
  "Glutamine" : "20g",
  "Vitamins" : 10}

dayof= {"BCAA" : "5g",
    "White" : "25g",
    "Glutamine" : "10g",
    "Vitamins" : 5}

username = input("What is your name?\n")
trening = input("Are you going to the gym today?\n")

def dosage():
    if trening == "yes":
       return (username + ", you have to take: " +
        "\nBCAA in dosage of: " + str(dayon["BCAA"]) +
        "\nWhite in dosage of: " +str(dayon["White"]) +
        "\nGlutamine in dosage of: " + str(dayon["Glutamine"]) +
        "\nVitamins in number of: " +str(dayon["Vitamins"]))
    elif trening == "no":
        return (username + ", you have to take: " +
        "\nBCAA in dosage of: " + str(dayof["BCAA"]) +
        "\nWhite in dosage of: " + str(dayof["White"]) +
        "\nGlutamine in dosage of: " + str(dayof["Glutamine"]) +
        "\nVitamins in number of: " + str(dayof["Vitamins"]))
    else:
        print ("Please answer yes or no")

print (dosage())

Solution

Here is the list of things I would improve:

  • for handling the proper answer on the "Are you going to the gym today?", I would start the while loop until "Yes" or "No" is entered



  • put the execution logic under if __name__ == '__main__':



  • define a message template with appropriate placeholders - format it with "day on" or "day off" objects and the entered username



  • define constants in upper case as per PEP8



  • let the "dosage" function return the dosage object - print the message outside of the function - for the better logic separation



  • add meaningful comments and docstrings



The code with all the suggested changes applied:

DAY_ON = {
    "BCAA": "10g",
    "White": "50g",
    "Glutamine": "20g",
    "Vitamins": 10
}

DAY_OFF = {
    "BCAA": "5g",
    "White": "25g",
    "Glutamine": "10g",
    "Vitamins": 5
}

MESSAGE_TEMPLATE = """
{username}, you have to take:
    BCAA in dosage of: {obj[BCAA]}
    White in dosage of: {obj[White]}
    Glutamine in dosage of: {obj[Glutamine]}
    Vitamins in number of: {obj[Vitamins]}
"""

def get_dosage(is_training_day):
    """Returns appropriate dosage depending on whether it is a training day or not"""
    return DAY_ON if is_training_day else DAY_OFF

if __name__ == '__main__':
    username = input("What is your name?\n")

    while True:
        choice = input("Are you going to the gym today?\n")

        if choice in ('Yes', 'No'):
            break
        else:
            print("You can only enter 'Yes' or 'No'.")

    dosage = get_dosage(is_training_day=choice == 'Yes')
    print(MESSAGE_TEMPLATE.format(username=username, obj=dosage))


Demo:

$ python3 test.py 
What is your name?
Chubaka
Are you going to the gym today?
What?
You can only enter 'Yes' or 'No'.
Are you going to the gym today?
No

Chubaka, you have to take:
    BCAA in dosage of: 5g
    White in dosage of: 25g
    Glutamine in dosage of: 10g
    Vitamins in number of: 5


If you are using Python3.6+, you can also use f-strings for string formatting:

dosage = get_dosage(is_training_day=choice == 'Yes')
print(f"""
{username}, you have to take:
    BCAA in dosage of: {dosage['BCAA']}
    White in dosage of: {dosage['White']}
    Glutamine in dosage of: {dosage['Glutamine']}
    Vitamins in number of: {dosage['Vitamins']}
""")

Code Snippets

DAY_ON = {
    "BCAA": "10g",
    "White": "50g",
    "Glutamine": "20g",
    "Vitamins": 10
}

DAY_OFF = {
    "BCAA": "5g",
    "White": "25g",
    "Glutamine": "10g",
    "Vitamins": 5
}

MESSAGE_TEMPLATE = """
{username}, you have to take:
    BCAA in dosage of: {obj[BCAA]}
    White in dosage of: {obj[White]}
    Glutamine in dosage of: {obj[Glutamine]}
    Vitamins in number of: {obj[Vitamins]}
"""


def get_dosage(is_training_day):
    """Returns appropriate dosage depending on whether it is a training day or not"""
    return DAY_ON if is_training_day else DAY_OFF


if __name__ == '__main__':
    username = input("What is your name?\n")

    while True:
        choice = input("Are you going to the gym today?\n")

        if choice in ('Yes', 'No'):
            break
        else:
            print("You can only enter 'Yes' or 'No'.")

    dosage = get_dosage(is_training_day=choice == 'Yes')
    print(MESSAGE_TEMPLATE.format(username=username, obj=dosage))
$ python3 test.py 
What is your name?
Chubaka
Are you going to the gym today?
What?
You can only enter 'Yes' or 'No'.
Are you going to the gym today?
No

Chubaka, you have to take:
    BCAA in dosage of: 5g
    White in dosage of: 25g
    Glutamine in dosage of: 10g
    Vitamins in number of: 5
dosage = get_dosage(is_training_day=choice == 'Yes')
print(f"""
{username}, you have to take:
    BCAA in dosage of: {dosage['BCAA']}
    White in dosage of: {dosage['White']}
    Glutamine in dosage of: {dosage['Glutamine']}
    Vitamins in number of: {dosage['Vitamins']}
""")

Context

StackExchange Code Review Q#154941, answer score: 7

Revisions (0)

No revisions yet.