Python Forum

Full Version: Invalid syntax defining a dictionary?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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)

@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 dictionary
So, 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
It looks OK, could you post the full unaltered error traceback.
Thanks
What i don´t understand is why does line 1 start with a @ ?
I know this to be used calling function decorators.
What is @interact() ?
(Nov-19-2019, 06:19 AM)ThomasL Wrote: [ -> ]What i don´t understand is why does line 1 start with a @ ? I know this to be used calling function decorators. What is @interact() ?

so, the Jupyter notebook i'm working with has already configured a library of widgets that allows me to put in the interactive sliders. The @interact is just the same line of code they show in a worked example, and it's worked for a previous exercise. I don't really know what the specific syntax means

(Nov-19-2019, 02:39 AM)Larz60+ Wrote: [ -> ]It looks OK, could you post the full unaltered error traceback.
Thanks
that's actually the full error I'm getting [Image: open?id=1UsLvesBwPxbAtSznWDjQqG7lc3M3flUI]
I can't test my suspicion right now but shouldn't function be right after decorator? In this code there is dictionary between decorator and function.
Yes, a decorator must come before a def statement for a function or method.
If you look here and read the docs
@interact() is a decorator for functions

There is no function following your @interact() that can be decorated.
Remove that @ character.

Quote:The @interact is just the same line of code they show in a worked example, and it's worked for a previous exercise. I don't really know what the specific syntax means

Just copy pasting code and not knowing what you are doing is wasting time. Ours and yours.