从同一个程序得到随机结果
我正在使用python请求库进行post调用。我的程序有时运行得很好,但有时会出现错误“UnboundLocalError:localvariable'response'referenced before assignment”。你知道吗
def test_fun():
try:
response = requests.get(f"{Base_URI}/Calls.json", auth=(AccSid, AccToken))
except Exception as err:
print(f'Other error occurred: {err}')
assert response.status_code == 200
"UnboundLocalError: local variable 'response' referenced before assignment"
就像Green斗篷人说的,当异常发生时,
response
变量是未定义的。这会导致错误。要解决此问题,可以将else
语句添加到try
:else
块在没有引发异常时运行。请注意,这与finally
不同,后者总是运行,而不管是否引发了错误。你知道吗如果请求未成功,可以使用
raise_for_status()
引发异常。你知道吗相关问题 更多 >
编程相关推荐