Nov-18-2019, 11:02 PM
Hi everyone! I'm currently working on an assignment for a Python course. The full task:
A county planning body has requested an interactive tool to visualise the population distribution in Cambridgeshire (by district) from 2011 to 2021 for different population growth rate scenarios in each district. It can be assumed that:
the growth rates are constant in each district;
the growth rate will not be negative in any district; and
the annual growth rate in any one district will not exceed 10%.
Building on the pie chart example with population data in the Activity notebook, create an interactive plot with:
A slider for the year (from 2011 to 2021); and
Sliders for the annual population growth for each district (in percentage), with an initial value of zero for each district.
I've started by defining initial data in a dictionary and writing a function that will return another dictionary
of population data based on growth rate and year values determined by sliders. The actual chart comes next. (Note: a library containing the "interact" widget has already been configured in the Jupyter notebook I'm using)
File "<ipython-input-32-a455a0991b1f>", line 3
pop_data_init = {"Cambridge City": 123900,
^
SyntaxError: invalid syntax
I don't get this error. I'm using the right brackets for the dictionary, the keys and values are separated by :, all entries are separated by a , and the variable pop_data_init is defined as a dictionary using a = sign. I genuinely have no idea what the invalid syntax is. Any help?
Thanks everyone!
P.S: feedback on the code as a whole is always appreciated, though this weird error is my priority rn
A county planning body has requested an interactive tool to visualise the population distribution in Cambridgeshire (by district) from 2011 to 2021 for different population growth rate scenarios in each district. It can be assumed that:
the growth rates are constant in each district;
the growth rate will not be negative in any district; and
the annual growth rate in any one district will not exceed 10%.
Building on the pie chart example with population data in the Activity notebook, create an interactive plot with:
A slider for the year (from 2011 to 2021); and
Sliders for the annual population growth for each district (in percentage), with an initial value of zero for each district.
I've started by defining initial data in a dictionary and writing a function that will return another dictionary
of population data based on growth rate and year values determined by sliders. The actual chart comes next. (Note: a library containing the "interact" widget has already been configured in the Jupyter notebook I'm using)
@interact(Cam_City = (0, 10, 0.5), East_Camshire = (0, 10, 0.5), Fenland = (0, 10, 0.5), Htdshire = (0, 10, 0.5), South_Camshire = (0, 10, 0.5), Year = (2011, 2021, 1)) #insert widgets needed to define growth rates pop_data_init = {"Cambridge City": 123900, "East Cambridgeshire": 83800, "Fenland": 95300, "Huntingdonshire": 169500, "South Cambridgeshire": 148800} #define initial population data for 2011 def popgrowth (Cam_City = 0, East_Camshire = 0, Fenland = 0, Htdshire = 0, South_Camshire = 0, Year = 2011): #define function for returning new population data growth = [Cam_City, East_Camshire, Fenland, Htdshire, South_Camshire] #store growth rates in a list, easier to use pop0 = [] for d in pop_data_init: pop0 += pop_data_init[d] #store population values for each district in list def pop_current(growth, pop0, Year): #new population is calculated using recursion, so define fxn specifically #for new population statistics if Year == 2011: pop = pop0 #stats for 2011 are initial stats else: pop = pop_current(growth, pop0, Year-1)*growth/100 #for 2012 and on, call back to last population list and #multiply through by the respective growth values, converted from % to decimal return pop #return a list of population numbers for the current year population_data = {"Cambridge City":pop_current(growth, pop0, Year)[0], "East Cambridgeshire": pop_current(growth, pop0, Year)[1], "Fenland": pop_current(growth, pop0, Year)[2], "Huntingdonshire": pop_current(growth, pop0, Year)[3], "South Cambridgeshire": pop_current(growth, pop0, Year)[4]} #rearrange as a dictionary return population_data #return resulting dictionarySo, I'm basically trying to troubleshoot this code bit by bit. Hopefully it will work as intended. However, the code throws an error 3 lines in, as follows:
File "<ipython-input-32-a455a0991b1f>", line 3
pop_data_init = {"Cambridge City": 123900,
^
SyntaxError: invalid syntax
I don't get this error. I'm using the right brackets for the dictionary, the keys and values are separated by :, all entries are separated by a , and the variable pop_data_init is defined as a dictionary using a = sign. I genuinely have no idea what the invalid syntax is. Any help?
Thanks everyone!
P.S: feedback on the code as a whole is always appreciated, though this weird error is my priority rn