0

New to Python

Path = "C:/Users/Kailash/Downloads/Results_For_Stride-Table.csv"
f = open(Path, "r")  # as infile: #open("C:/Users/Kailash/Downloads/25ft_output.csv", "w") as outfile:
counter = 0
n = 0
distance = float
print("n =", n)
while True:
    counter += 1
    #print("Counter = ", counter)
    lines = f.readline()
    Stride_Length = lines.split(", ")
    # if (distance > 762):
    #    Delta_t = distance
    #    print("Pre-Delta_t = ", Delta_t)
    distance[n] += float(Stride_Length[3])
    #n += 1
    if distance > 762:
        Delta_t = 762 - distance[n - 1]
        print("Delta_t = ", Delta_t)
        Residual_distance = distance - 762
        print("Residual_distance = ", Residual_distance)
        counter = 0
        distance = Residual_distance
        print("Counter & Distance RESET!")
    print(distance)

I am getting a TypeError: 'type' object is not subscriptable in line 15:

distance[n] += float(Stride_Length[3])

Any idea why am I seeing this?

13
  • 1
    you may want to start with distance = float(0) ? then, what the n loop part should do with distance ? Commented Jul 8, 2017 at 0:22
  • this question is answered: take a look at this link. Commented Jul 8, 2017 at 0:26
  • @PRMoureu: I tried replacing distance = float with float(0) but that did not solve. 'n' has to increment everytime. Sorry the # has to be removed. Commented Jul 8, 2017 at 0:27
  • you want to use distance as a dict, a list or a number ? for a number, you have to remove all [n] Commented Jul 8, 2017 at 0:33
  • @PRMoureu: distance is just a variable name I am using. It can be changed to anything I want. I want to use distance as a float number. I am copying float(Stride_Length[3] into distance[0]. Commented Jul 8, 2017 at 0:36

1 Answer 1

0

For me I go the same error once I try to access model class attributes like if it was dictionary dimension['dimension_id'] though dimension type is app.models.evaluation.DimensionsEvaluation object so I got the error

TypeError: 'DimensionsEvaluation' object is not subscriptable

to fix this I had to get the dictionary field from the model class using .__dict__ and access my attribute normally like above:

...
for dimension in evaluation_dimensions:
    print(f"\n dimension_id: {dimension['dimension_id']}\n") #TypeError: 'DimensionsEvaluation' object is not subscriptable


for dimension in evaluation_dimensions:
    dimension_dic = dimension.__dict__
    print(f"\n dimension_id: {dimension_dic['dimension_id']}\n") #dimension_id: 1
...

.


Complete code:

async def getAssessments(client_id: UUID, db: Session = Depends(get_async_session)):
    evaluation_id_data = await getEvaluationId(client_id, db)
    evaluation_id = evaluation_id_data['data']
    evaluation_dimensions_data = await getEvluationDimensions(evaluation_id, db)
    evaluation_dimensions = evaluation_dimensions_data['data']

    assessment = []
    for dimension in evaluation_dimensions:
        print(f"\n\n dimension {dimension} \n\n")
        print(f"\n\n dimension dict {dimension.__dict__} \n\n")

the result is

dimension <app.models.evaluation.DimensionsEvaluation object at 0x10d8def50> 

 dimension dict {'_sa_instance_state': <sqlalchemy.orm.state.InstanceState object at 0x10d8def90>, 'selected': True, 'dimension_id': 2, 'evaluation_id': 6, 'id': 16, 'questions': '[152,153]'} 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.