请问下面这个python报错要怎么解决,需要如何去定义初始化这个america?
你if判断都没有成立,america没有创建
if topcountry[i]["name"] == '上海':
america = topcountry[i]
你循环中所有 topcountry[i]["name"] 都不等于 '上海':
一直没有执行 america = topcountry[i]
america 就是未定义
对。america是在for循环的if语句中的,只在topcountry[i][ 'name ' ] == '上海'
时成立,而有可能所有topcountry的元素都不符合这一条件,于是america没有被创建。