[英]local variable '?' referenced before assignment
我是Python和一般编程的新手。 我想知道如何摆脱这个编译错误
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)
感谢您的帮助,这是我的第一篇文章。
修改要分配给列表的变量语句。
alive = ["alive", "sedentary"]
very_low = ["low", "very low"]
active = ["active", "very active"]
将所有==
替换为in
,
包括缺少的elif
语句。
elif (activity_level in very_low) and (is_smoker is True):
xer = "high" # or medium or whatever
注意:为减少冗余,可以将and is_smoker
如果为True
, and not is_smoker
为False
and not is_smoker
可以。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.