【发布时间】:2018-03-07 09:03:30
【问题描述】:
我通常会写出可耻的糟糕的代码,但这次我改变了主意,我想了解 Python 函数。
所以我正在尝试重写过去的一些脚本。我不明白为什么这段代码不执行:
import requests #
import json
def Status_OK():
for SQL_element in response_data1['results']:
SQL_Place_ID = SQL_element['place_id']
def get_place_for_show():
url1 = 'https://maps.googleapis.com/maps/api/place/nearbysearch/json?types=movie_theater&location=-36.86667,174.76667&radius=25000&key=MyGoogleKey'
response1 = requests.get(url = url1)
response_data1 = response1.json()
if response_data1['status'] == 'OK':
Status_OK()
get_place_for_show()
我的 HAL9000 回复这个错误:
Traceback (most recent call last):
File "Test.py", line 16, in <module>
get_place_for_show()
File "Test.py", line 14, in get_place_for_show
Status_OK()
File "Test.py", line 5, in Status_OK
for SQL_element in response_data1['results']:
NameError: name 'response_data1' is not defined
什么? NameError: name 'response_data1' 没有定义?
上面定义了一行!
该代码应该只打印 Google Place_ID 的列表,我知道该代码有效,因为我在另一个脚本上使用它但没有函数
【问题讨论】:
-
response_data1是get_place_for_show()中的一个局部变量。它在Status_OK()中不可用。您需要将其作为参数传递给函数。 -
你需要了解变量作用域。
标签: python json function google-maps-api-3