-3

I'm coding for a project at school (GCSEs) and we have been tasked that the user will enter the value of a population and we have to code a program where python displays a model to show the population after a set number of generations. Once the user has entered their desired values I need to display it in a table-like format that shows the generation number an the population to the respective age range (juvenile, adult, senile). To make the table I used an array but defined it earlier at the beginning of the code. When I try to append the values to the code I get this error ''float' object is not subscriptable'

this is my code of me trying to add to the array:

def run_model():

    print ("*"*10, "Running the Model", "*"*10)
    time.sleep(2)

    for x in range (1, gen_no):
        if(x !=0):
            juve_values.append ( float(adult_pop[x-1])*birth_rate)
            adult_values.append ( float(juve_pop[x-1])*juve_suv)
            senile_values.append ( float(sen_pop[x-1])*sen_suv+adult_pop[x-1]*adult_suv)

    for y in range (1, gen_no):
       print(str(juve_pop[y]) + " "*5 + str(adult_pop[y]) + " "*5 + str(sen_pop[y]))

when you answer please make it as basic as possible as I'm not an advanced coder.

Thanks

  • 3
    "it won't look believable when I had in my work" - are you asking us to write code that you'll just dump into your homework verbatim? – user2357112 Jul 11 '16 at 21:45
  • See this question http://stackoverflow.com/questions/19991591/typeerror-float-object-is-not-subscriptable – Alden Jul 11 '16 at 21:47
  • 1
    What is the type of `adult_pop`, `juve_pop` and `sen_pop`? – John Gordon Jul 11 '16 at 21:53
  • 1
    no i'm just asking what could i do to solve it because this is just one portion of my work that i can't seem to understand and my classmates don't undertstand. also the intent behind my clause is that if the coding is too complicated for me to understand and i put it in without knowing what it means because it is too complicated then that is just me plagiarising your work – TheBattleFielder241 Jul 11 '16 at 21:54
  • adult_pop, juve_pop, sen_pop are variables that the user enters the value for – TheBattleFielder241 Jul 11 '16 at 21:56
  • Yes, but what is the _type_, as I asked? String, float, integer, list, what? – John Gordon Jul 11 '16 at 22:08
  • sorry, they are floats and integers – TheBattleFielder241 Jul 11 '16 at 22:11

1 Answers1

0

I'm guessing adult_pop and the other population variables are not something that is subscriptable (i.e lists, tuples, dicts, etc) and are either ints or floats instead. That means you are trying to enter a type that does not allow that kind of examination, hence you are getting the error.

I think this answer will help you with subscriptablity.

Community
  • 1
  • 1
af3ld
  • 782
  • 8
  • 30