'return file inside an if statement
I am trying to return only the response of status code that is 200, and its inside an if statement, when I put the return preddata inside the if statement, nothing gets returned, when I put it outside, I get an error that UnboundLocalError: local variable 'preddata' referenced before assignment, how can I solve this, this is the code:
def obtain_data(url):
response = requests.get(url)
if response.status_code == 200:
# getting data in the json format
preddata = response.json()
preddata=(pd.DataFrame(preddata['hourly']))
return preddata
Solution 1:[1]
If you got None
returned, it probably means that response.status_code == 200
is False
.
(Other option would be that DataFrame constructor generates None, but not possible in the context.)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Floh |