Home > Enterprise >  Get data from a dictionary into a mathematic function?
Get data from a dictionary into a mathematic function?

Time:11-20

I´ve been trying a lot but can´t come to the finsih:

sports_info = {"bicycling": {"met": 14,
                         "max_min_of_execution": 60},
            "crunches": {"met": 5,
                         "max_min_of_execution": 15},
            "swimming": {"met": 9.5,
                         "max_min_of_execution": 30},
            "push ups": {"met": 8,
                         "max_min_of_execution": 15},
            "sitting": {"met": 1,
                        "max_min_of_execution": -1}}

I want to get the informations from sports_info into the following functions:

def met_to_burned_calories_per_minute(met: float, weight: float) -> float:
    return (met * 3.5 * weight)/200

def do_sport_and_burn_calories(calories_to_burn: int, sports_info: dict[str, dict[str, int]],
    weight = float(65)):
    calories_to_burn = 1560

and create an output similar to this one:

print("To burn <calories_to_burn> calories with your given sports you have to do" 
      "<mins_of_sport_1> mins of <sport_1>, <mins_of_sport_2> mins of <sport_2><mins_of_sport_n> mins of <sport_n>!"
      "At the end you still need to burn <unburned_calories> calories.")

I tried different ways to get the sports_info in the given formula, but wasn´t successful, how can I best start with my code? I thank you for any kind of help in advance!

CodePudding user response:

You can use f-strings and write your code in a way like so:

def do_sport_and_burn_calories(calories_to_burn: int, sports_info: dict[str, dict[str, int]], weight: float):
    print(f'To burn {calories_to_burn} calories with your given sports you have to do')
    todo_list = [f'{sports_info[sport]["max_min_of_execution"]} mins of {sport}' for sport in sports_info]
    todo_results = [met_to_burned_calories_per_minute(sports_info[sport]['met'], weight) for sport in sports_info]
    print(', '.join(todo_list) '!')
    print(f'At the end you still need to burn {calories_to_burn - sum(todo_results)} calories.')
          
>>> do_sport_and_burn_calories(1560, sports_info, 65)
To burn 1560 calories with your given sports you have to do
60 mins of bicycling, 15 mins of crunches, 30 mins of swimming, 15 mins of push ups, -1 mins of sitting!
At the end you still need to burn 1517.34375 calories.
  • Related