从字典中获取数据到数学函数中?
Get data from a dictionary into a mathematic function?
我已经尝试了很多,但还是无法完成:
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}}
我想从 sports_info 获取信息到以下函数中:
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
并创建类似于此的输出:
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.")
我尝试了不同的方法来获得给定公式中的 sports_info,但没有成功,
我怎样才能最好地开始我的代码?感谢您提前提供的任何帮助!
您可以使用 f-strings 并按如下方式编写代码:
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.
我已经尝试了很多,但还是无法完成:
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}}
我想从 sports_info 获取信息到以下函数中:
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
并创建类似于此的输出:
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.")
我尝试了不同的方法来获得给定公式中的 sports_info,但没有成功, 我怎样才能最好地开始我的代码?感谢您提前提供的任何帮助!
您可以使用 f-strings 并按如下方式编写代码:
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.