简体   繁体   English

局部变量“?” 分配前引用

[英]local variable '?' referenced before assignment

I am new to Python and programming in general. 我是Python和一般编程的新手。 I would like to know how to get rid of this compile error 我想知道如何摆脱这个编译错误

def health_risk(activity_level, is_smoker):
    """Counts the aliveness of a person"""  
    alive = "alive?" or "sedentary"
    very_low = "low" or "very low"
    active = "active" or "very active"    
    if (activity_level in alive) and is_smoker is True:
        xer = "extreme" 
    elif (activity_level in active) and is_smoker is True:
        xer = "medium"    
    elif (activity_level == alive) and (is_smoker is False):
        xer = "high"     
    elif (activity_level == very_low) and (is_smoker is False):
        xer = "medium"  
    elif activity_level == active and is_smoker is False:
        xer = "low"          
    return xer
level = health_risk('low', True)
print(level)

Thanks for the help and this is my first post, thanks. 感谢您的帮助,这是我的第一篇文章。

Revise your variable statements to be assigned to lists. 修改要分配给列表的变量语句。

alive = ["alive", "sedentary"]
very_low = ["low", "very low"]
active = ["active", "very active"]

Replace all == with in , 将所有==替换为in

Include the missing elif statement. 包括缺少的elif语句。

elif (activity_level in very_low) and (is_smoker is True):
    xer = "high" # or medium or whatever

note: to reduce redundancy, you could just put and is_smoker if it's True and and not is_smoker if it's False . 注意:为减少冗余,可以将and is_smoker如果为Trueand not is_smokerFalse and not is_smoker可以。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM