Python 3.x 自检码

Python 3.x 自检码,python-3.x,function,Python 3.x,Function,我是编程新手,目前正在自学如何编写代码。老实说,我甚至不知道这是不是发布此类问题的合适地方 但是,我刚刚做了一个men_macro()函数来计算男性卡路里 def men_macro(height, weight, age, activity_level, gender='Male'): ''' Initializing the activity_level ''' active_level = {'Sedentary': 1.2, 'Lightly Active

我是编程新手,目前正在自学如何编写代码。老实说,我甚至不知道这是不是发布此类问题的合适地方

但是,我刚刚做了一个men_macro()函数来计算男性卡路里

def men_macro(height, weight, age, activity_level, gender='Male'):
''' Initializing the activity_level '''
active_level = {'Sedentary': 1.2,
                'Lightly Active': 1.375,
                'Moderately Active': 1.55,
                'Very Active': 1.725,
                'Extremely Active': 1.9}

# Creating men Macro Formula using Mifflin-St Jeor Equation
macro = ((10 * weight) + (6.25 * height) - (5 * age) + 5)

# Check if activity_level argument is in the active_level dictonary
for key, value in active_level.items():
    if activity_level == key:
        return macro * value
这个函数工作正常,我的宏也很好


你们对代码有什么看法,只是想听听你们的意见,我还有什么需要改进的。

我认为活动级别最好去掉函数,它独立于局部变量。 最后一个块我将按如下方式重做:

if activity_level in active_level:
    return macro*active_level[activity_level]

如果此代码已在运行,并且您要求他人查看您的代码,请查看其他StackExchange站点:。确保遵守他们的指示。堆栈溢出用于询问特定问题和解决特定问题。@GinoMempin,谢谢,我会检查它的!