UnboundLocalError: local variable ‘bmi’ referenced before assignment line 38

huangapple go评论136阅读模式
英文:

UnboundLocalError: local variable 'bmi' referenced before assignment line 38

问题

以下是代码的中文翻译:

from dash import Dash, dcc, html, Input, Output

app = Dash(__name__)

app.layout = html.Div([
    html.H1("BMI计算器"),
    html.Div([
        html.H2('输入体重'),
        dcc.Input(id='weight', value=160, type='number'),
        dcc.RadioItems(options=[{'label': '磅', 'value': 'lbs'}, {'label': '千克', 'value': 'kgs'}],
                       value='lbs',
                       id='weight_unit'),
        html.H2('输入身高'),
        dcc.Input(id='height', value=5.83333333, type='number'),
        dcc.RadioItems(options=[{'label': '英尺', 'value': 'ft'}, {'label': '米', 'value': 'm'}],
                       value='ft',
                       id='height_unit'),
    ]),
    html.Br(),
    html.H1("你的估计身体质量指数是:"),
    html.H1(id='bmi'),

])

@app.callback(
    Output(component_id='bmi', component_property='children'),
    Input(component_id='weight', component_property='value'),
    Input(component_id='weight_unit', component_property='value'),
    Input(component_id='height', component_property='value'),
    Input(component_id='height_unit', component_property='value'),
   )

def update_output_div(weight, weight_unit, height, height_unit):
    if weight_unit == 'lbs' and height_unit == 'ft':
        bmi = 703 * (weight / (height * 12) ** 2)
    if weight_unit == 'kgs' and height_unit == 'm':
        bmi = (weight) / ((height) ** 2)
    return bmi

if __name__ == '__main__':
    app.run_server(debug=True)

希望这有所帮助!如果您有任何其他问题,请随时提出。

英文:

I am making a BMI calculator with plotly Dash. I'm trying to code it where the user choses the unit of the input for weight and height, however its throwing an error on line 38:

UnboundLocalError: local variable 'bmi' referenced before assignment

from dash import Dash, dcc, html, Input, Output

app = Dash(__name__)

app.layout = html.Div([
    html.H1("BMI Calculator"),
    html.Div([
        html.H2('Enter your weight'),
        dcc.Input(id = 'weight', value = 160, type = 'number'),
        dcc.RadioItems(options = [{'label': 'pounds', 'value': 'lbs'}, {'label': 'kilograms', 'value': 'kgs'}], 
                       value = 'lbs',
                       id = 'weight_unit'),
        html.H2('Enter your height'),
        dcc.Input(id = 'height', value = 5.83333333, type = 'number'),
        dcc.RadioItems(options = [{'label': 'feet', 'value': 'ft'},{'label': 'meters', 'value': 'm'}], 
                       value = 'ft', 
                       id = 'height_unit'),      
    ]),
    html.Br(),
    html.H1("Your estimated body mass index is:"),
    html.H1(id = 'bmi'),

])

@app.callback(
    Output(component_id = 'bmi', component_property = 'children'),
    Input(component_id  = 'weight', component_property = 'value'),
    Input(component_id  = 'weight_unit', component_property = 'value'),
    Input(component_id  = 'height', component_property = 'value'),
    Input(component_id  = 'height_unit', component_property = 'value'),
   )
    
def update_output_div(weight, weight_unit, height, height_unit):
    if weight_unit == 'lbs' and height_unit == 'ft':
        bmi = 703 * (weight/(height * 12)**2)
    if weight_unit == 'kgs' and height_unit == 'm':
        bmi = (weight)/((height)**2)
    return bmi 

if __name__ == '__main__':
    app.run_server(debug=True)

答案1

得分: 1

使用return语句对在if块内分配的变量进行操作意味着该变量仅在满足条件时才会被赋值。否则,当代码未运行时,变量bmi不会被定义,也无法返回。

要解决这个问题,一种方法是在if语句之前分配一个值。

def update_output_div(weight, weight_unit, height, height_unit):
    bmi = None
    if weight_unit == 'lbs' and height_unit == 'ft':
        bmi = 703 * (weight / (height * 12)**2)
    if weight_unit == 'kgs' and height_unit == 'm':
        bmi = (weight) / ((height)**2)
    return bmi
英文:

Using return on a variable that was assigned within an if block means that the variable will only be assigned if that condition is met. Otherwise, when the code doesn't run, the variable bmi does not get defined, and cannot be returned.

To resolve the problem, one way is to assign a value before the if statement.

def update_output_div(weight, weight_unit, height, height_unit):
    bmi = None
    if weight_unit == 'lbs' and height_unit == 'ft':
        bmi = 703 * (weight/(height * 12)**2)
    if weight_unit == 'kgs' and height_unit == 'm':
        bmi = (weight)/((height)**2)
    return bmi 

huangapple
  • 本文由 发表于 2023年3月12日 13:25:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/75711210.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定