Python Forum
Invalid syntax defining a dictionary?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Invalid syntax defining a dictionary?
#1
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
Reply
#2
It looks OK, could you post the full unaltered error traceback.
Thanks
Reply
#3
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() ?
Reply
#4
(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]
Reply
#5
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.
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#6
Yes, a decorator must come before a def statement for a function or method.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#7
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Invalid syntax Slome 2 1,971 May-13-2022, 08:31 PM
Last Post: Slome
  print(f"{person}:") SyntaxError: invalid syntax when running it AryaIC 11 16,375 Nov-07-2020, 10:17 AM
Last Post: snippsat
  Invalid syntax error, where? tucktuck9 2 3,432 May-03-2020, 09:40 AM
Last Post: pyzyx3qwerty
  Syntax Error: Invalid Syntax in a while loop sydney 1 4,086 Oct-19-2019, 01:40 AM
Last Post: jefsummers
  Help with syntax to sort dictionary by value Mark17 2 2,161 Sep-17-2019, 02:19 PM
Last Post: Mark17
  [split] Please help with SyntaxError: invalid syntax Mason 1 2,210 Apr-28-2019, 06:58 PM
Last Post: Yoriz
  SyntaxError: Invalid syntax in a while loop ludegrae 3 14,758 Dec-18-2018, 04:12 PM
Last Post: Larz60+
  SyntaxError: invalid syntax at run .py tuxo9999 10 7,281 Aug-23-2018, 03:58 PM
Last Post: Axel_Erfurt
  Homework: Invalid syntax using if statements chehortop 3 3,668 Mar-01-2018, 04:38 AM
Last Post: micseydel
  invalid syntax error penlemon 4 3,842 Jan-27-2018, 02:46 PM
Last Post: sparkz_alot

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020