不确定为什么将名为“ CityPopYear2”的变量视为无效语法。我的目标是从我读取的csv文件中的数据中简单地找出一个城市在2年内的人口差异。因此,“ CityPopYear1”和“ CityPopYear2”仅用于减法公式中,代表2年各自的人口。不确定为什么将这样的简单方程式视为语法错误。
def CityPopChange():
f = open("CityPop.csv","r")
header = f.readline()
header = header.strip().split(",")
CityPop = {}
for line in f:
record = line.strip().split(",")
CityPop[record[4]] = record
CityName = raw_input("What city would you like to view?: ")
CityYear1 = raw_input("Please enter the earliest year you would like studied: ")
CityYear2 = raw_input("Please enter the most recent year you would like studied: ")
i = 0
while i < len(header):
if header[i] == CityYear1:
break
i = i +1
j = 0
while j < len(header):
if header[i] == CityYear2:
break
j = j + 1
CityPopYear1 = float((CityPop[CityYear1][i])
CityPopYear2 = float((CityPop[CityYear2][j])
PopChange = CityPopYear2 - CityPopYear1
print "The difference in population in", CityName, "between", CityYear1, "and", CityYear2, "is", PopChange, "."
CityPopChange()