Python变量奇怪语法错误

Python变量奇怪语法错误,python,string,variables,Python,String,Variables,我的python代码: import xml.etree.ElementTree as ETree tree = ETree.ElementTree(file="CountryData.xml") root = tree.getroot() textfile = open("Output.txt", "w") print("Population densities:") for i in root: density = str(float((i.attrib["areaInS

我的python代码:


import xml.etree.ElementTree as ETree

tree = ETree.ElementTree(file="CountryData.xml")
root = tree.getroot()

textfile = open("Output.txt", "w")


print("Population densities:")

for i in root:
    density = str(float((i.attrib["areaInSqKm"])/float(i.attrib["population"]))
    n = (i.get("countryName"))
    b = n + ": " + density + "people/km2 \n"
    textfile.write(b)

textfile.close()
正在给我以下错误:

      File "G:\CountryData.py", line 13
         n = i.get("countryName"))
         ^
    SyntaxError: invalid syntax
我尝试了多种方法和其他写作方法,但我不知道如何解决这个问题。有人看到我的错误吗


ps,在我添加关于人口密度的部分之前,它工作得很好

str(float((i.attrib["areaInSqKm"])/float(i.attrib["population"]))
应该是:

str(float((i.attrib["areaInSqKm"])/float(i.attrib["population"])))
您缺少1个字符:)


改为使用以下行(您缺少右括号):


第12+1行缺少一个
来获得另一个正确答案+1来获得第三个好答案
density = str(float((i.attrib["areaInSqKm"])/float(i.attrib["population"]))

density = str(float((i.attrib["areaInSqKm"])/float(i.attrib["population"])))
density = str(float((i.attrib["areaInSqKm"])/float(i.attrib["population"])))