1

I am teaching myself Python and am trying out a challenge I found to create a quote program for a gardener. I have almost all of it working and have added in iteration so that the user can make more than one quote without re-starting the program.

It produces the quote perfectly the first time but on the second run it presents this error:

Traceback (most recent call last):

  File "/Users/shaunrogers/Desktop/Plymstock Prep/GCSE CS/SOL/Controlled Assessment/Sample Papers Solutions/gardening Task 2.py", line 105, in <module>

    lawn = m2_items("lawn",0)
  File "/Users/shaunrogers/Desktop/Plymstock Prep/GCSE CS/SOL/Controlled Assessment/Sample Papers Solutions/gardening Task 2.py", line 23, in m2_items

    minutes = area*time[index]

TypeError: 'float' object is not subscriptable

I have the following code as a function that is producing the error:

def m2_items (item,index):

    global costs, time, LABOUR

    length = int(input("How long is the "+ item+"?\n"))

    width = int(input("How wide is the "+item+"?\n"))

    area = length*width

    cost_m2 = float(costs[index])

    total_cost = area*cost_m2

    minutes = area*time[index]

    hours = int(minutes/60)

    labour = LABOUR*hours

    labour_cost=round(labour,2)

    m2_details = [area, cost_m2, total_cost,hours, labour_cost]

    return m2_details

I have tried re-setting the local variables on the running of the function (but I didn't think this was needed as the variables should be removed from memory once the function has run).

I hope the question is clear and that I can get some insight. To re-iterate, what I want the program to do is allow me to call this function multiple times.

3
  • 1
    Isn't it clear that area*time returns float number and number is not a collection (list, dictionary, set, string etc. - for these types getting an element at [index] is a valid operation)? And nothing stops you to call this function multiple times. This is not the complete code. Show us how you are calling this function. Commented Jul 28, 2015 at 8:26
  • 1
    What is time global and where is it defined? Commented Jul 28, 2015 at 8:30
  • time is a float type object but you're attempting to access it like a dictionary or list. Should time have a different type? Or did you misunderstand the syntax when doing time[index]? Commented Jul 28, 2015 at 8:57

1 Answer 1

1

You are using the global time variable, which is initially subscriptable (probably an array). As your program continues, some other part of your code will assign a new value to time, maybe accidentally because you wrote time = some_calculation() instead of time[i] = some_calculation(), or maybe you are using the name time somewhere else without realizing it's already in use.

Do a search for all the places where you use the name time and you will probably find your error.

This is a common problem with global variables. Sometimes something updates them from another part of the code, and the error will sneak up on you like this.

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you Andre, That was exactly it. As soon as i read your answer it clicked an sorted my problem. Thank you for your help.
So glad to hear you solved this! :) Learning Python is fun, but you get stuck sometimes. I hope you find StackOverflow a good place to learn. If you found this answer helpful you can mark it as accepted.
Hi Andre, I have marked this and answered. Yours was by far the most helpful. I think I couldn't see the wood for the trees and within the first two sentences of your answer I knew exactly what I had done wrong. Thanks again

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.