不用担心:)欢迎使用Python!它抛出这个错误是因为它正在寻找一个不存在的全局变量——而它不存在的原因是因为你没有命中if type == "accounts"
健康)状况!
尝试这个:
for i in included:
global signs,accounts, regions
global sign_name, acc_name, rg_name
regions = "no region yet"
acc_name = "no acc_name yet"
if type == "regions"
regions = i
rg_name = regions['data']['region']
if type == "accounts"
accounts = i
acc_name = accounts['data']['account']
print("Stopping account " + acc_name + " in region " + rg_name)
这将清除错误,并至少让您看到可能出现的其他错误:)
我还要指出,正如我确信您会从其他人那里听到的那样,您没有理由在这种情况下声明全局变量。它最初是说“找不到全局变量”,因为在你输入之前global
关键字,它没有触发if
声明,所以首先它检查了locals()
变量,但没有找到它,搜索了globals()
变量,并没有发现它被踢和错误。
您可以删除global
变量,它会像这样工作得很好:
for i in included:
regions = "no region yet"
acc_name = "no acc_name yet"
if type == "regions"
regions = i
rg_name = regions['data']['region']
if type == "accounts"
accounts = i
acc_name = accounts['data']['account']
print("Stopping account " + acc_name + " in region " + rg_name)
另一个快速说明,从来没有type
作为变量...使用type_
反而。原因是type
is a builtin
Python 函数,如果你使用type
作为变量,您不小心为该内置名称起了别名。
最后,稍微清理一下脚本:
# only use "i" when you're using numbers, otherwise just call it
# the name of the data you're using :)
for account_data in included:
regions = "no region yet"
acc_name = "no acc_name yet"
if type_ == "regions"
rg_name = account_data['data']['region']
if type_ == "accounts"
acc_name = account_data['data']['account']
# here's an example of Pythonic string formatting :)
print("Stopping account {} in region {}".format(acc_name, rg_name))