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


Messages In This Thread
Invalid syntax defining a dictionary? - by ep595 - Nov-18-2019, 11:02 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Invalid syntax Slome 2 2,017 May-13-2022, 08:31 PM
Last Post: Slome
  print(f"{person}:") SyntaxError: invalid syntax when running it AryaIC 11 16,523 Nov-07-2020, 10:17 AM
Last Post: snippsat
  Invalid syntax error, where? tucktuck9 2 3,450 May-03-2020, 09:40 AM
Last Post: pyzyx3qwerty
  Syntax Error: Invalid Syntax in a while loop sydney 1 4,119 Oct-19-2019, 01:40 AM
Last Post: jefsummers
  Help with syntax to sort dictionary by value Mark17 2 2,182 Sep-17-2019, 02:19 PM
Last Post: Mark17
  [split] Please help with SyntaxError: invalid syntax Mason 1 2,223 Apr-28-2019, 06:58 PM
Last Post: Yoriz
  SyntaxError: Invalid syntax in a while loop ludegrae 3 14,808 Dec-18-2018, 04:12 PM
Last Post: Larz60+
  SyntaxError: invalid syntax at run .py tuxo9999 10 7,388 Aug-23-2018, 03:58 PM
Last Post: Axel_Erfurt
  Homework: Invalid syntax using if statements chehortop 3 3,689 Mar-01-2018, 04:38 AM
Last Post: micseydel
  invalid syntax error penlemon 4 3,884 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