我在我的 alexa 技能中不断收到带有语音输出的 UnboundLocalError

我开始收到此错误,但它之前工作过,所以我不确定发生了什么变化。


文件“/var/task/lambda_function.py”,第 126 行,在 get_elevator_status 语音输出 += 'On' + 电梯 ['line'] + ' 在车站 ' + 电梯 ['车站'] + \ UnboundLocalError: 局部变量 'speech_output ' 在赋值前引用


def get_elevator_status():

    session_attributes = {}

    card_title = "Septa Elevator Status"

    should_end_session = True


    response = urllib2.urlopen(API_BASE_URL + "/elevator")

    septa_elevator_status = json.load(response)


    if septa_elevator_status['meta']['elevators_out'] == 0:

        speech_output = 'All Elevators are currently operational'

    else:

        for elevators in septa_elevator_status['results']:

            speech_output += 'On' + elevators['line'] + ' at station ' + elevators['station'] + \

                ' the ' + elevators['elevator'] + \

                ' elevator has ' + elevators['message'] + ' . '


    return build_response(session_attributes, build_speechlet_response(

        card_title, speech_output, reprompt_text, should_end_session))


撒科打诨
浏览 190回答 2
2回答

汪汪一只猫

您尚未定义speech_outputelse 情况下的内容。speech_output定义为 if septa_elevator_status['meta']['elevators_out'] == 0,否则不定义。else 子句也需要定义speed_output,或者需要在 if / else 块之前定义。请记住:a += bmeansa是任何东西,加上b。因此,如果a未定义,则会引发错误。据推测,这在之前不会是错误的,因为 if 案例评估为 True 这意味着speech_output正在定义,但现在 if 案例不是 True 所以speech_output不再定义。

米脂

检查内容septa_elevator_status['results']。如果它是空的(大概就是这样),for 循环中的代码将不会执行,speech_output也不会被定义。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python