i have example text from Python which i a working on.
Afghanistan:32738376
Akrotiri:15700
Albania:3619778
Algeria:33769669
American Samoa:57496
Andorra:72413
Angola:12531357
Anguilla:14108
Antigua and Barbuda:69842
Argentina:40677348
Armenia:2968586
Aruba:101541
Australia:20600856
Austria:8205533
Azerbaijan:8177717
I have this code to make a dictionary using the country names and population.
dct = {}
for line in infile:
line = line.strip()
words = line.split(":")
countryname = words[0]
population = int(words[1])
dct[countryname] = population
When i print population, it prints all the values but then i get an population = int(words[1]) - IndexError: list index out of range. I don't understand how i am getting this error, especially as when i print countryname, it is absolutely fine, the error only occurs with population. Python has to access the same amount of lines for both variables but it seems like with population its trying to access more lines, which i do not understand because it doesn't do this for countryname. Any ideas on why this is occurring.