3

I'm trying to modify the attributes of an object stored in an array.

Specifically, I have an array of qb "tuples" that store name, touchdowns, etc. (and the array definitely holds the values correctly and I normally refer to them as pp.passing_yds) and I want to for example multiply the amount of touchdowns this qb has by 10

Here is my attempt:

for pp in qb:
    qb[pp.passing_tds] *= 10

However, I get an index out of bounds error here which makes it seem like I'm multiplying the index when I'm actually trying to multiply the attribute.

2
  • ok so i tried another way for pp in qbPastFive: qbPastFive[pp].passing_yds *= 10 and that says list indicies must be integers and not my data type Commented Oct 29, 2015 at 14:46
  • 1
    What happens when you just use "pp.passing_tds *= 10" in your for loop? Commented Oct 29, 2015 at 14:48

3 Answers 3

4

As already mentioned in the comments: I think all you have to do is to modify your for-loop to:

for pp in qb:
    pp.passing_tds *= 10

assuming that qp is your list of objects. Then pp is an object and you directly change its attribute.

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

2 Comments

great ill try that out, I accidently delete my python file from my server with no back up so hopefulyy i can find a way to get it back
Good luck! I highly recommend using github, gitlab or something comparable to avoid such troubles. And let me know if it then works for you and whether you need any further explanation!
1

Try:

for pp in qb:
    pp.passing_tds *= 10

What you tried to do was (assuming pp.passing_tds = 45)

qb[45] # <- the 45th item in the qb list, which may not exist, if qb is a small list

Comments

0

Try:

for i in range(len(qb)):
    qb[i].passing_tds *= 10

If you use:

for pp in pb:
    pp.passing_tds *= 10

it won't change the attribute in the array because in a for loop, it creates an independent variable(pp) which won't influence the original array.

Comments

Your Answer

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