Velvet Star Monitor

Standout celebrity highlights with iconic style.

news

How to append float to list?

Writer Andrew Mclaughlin

I want to append float to list, but I got an error like this:

<ipython-input-47-08d9c3f8f180> in maxEs() 12 Es = lists[0]*0.3862 + lists[1]*0.3091 + lists[2]*0.4884 13 aaa = []
---> 14 Es.append(aaa) 15 AttributeError: 'float' object has no attribute 'append'

I guess I can't append float to list. Can I add floats to list another way?

This is my code:

import math
def maxEs(): for a in range(1, 101): for b in range(1,101): for c in range(1,101): if a+b+c == 100 : lists = [] lists.append(a*0.01) lists.append(b*0.01) lists.append(c*0.01) Es = lists[0]*0.3862 + lists[1]*0.3091 + lists[2]*0.4884 aaa = [] Es.append(aaa)
3

2 Answers

I don't know what you want, but you are trying to append a list to a float not the other way round.

Should be

aaa.append(Es)
0

The other answer already explained the main problem with your code, but there is more:

  • as already said, it has to be aaa.append(Es) (you did it right for the other list)
  • speaking of the other list: you don't need it at all; just use the values directly in the formula
  • aaa is re-initialized and overwritten in each iteration of the loop; you should probably move it to the top
  • you do not need the inner loop to find c; once you know a and b, you can calculate c so that it satisfies the condition
  • you can also restrict the loop for b, so the result does not exceed 100
  • finally, you should probably return some result (the max of aaa maybe?)

We do not know what exactly the code is trying to achieve, but maybe try this:

def maxEs(): aaa = [] for a in range(1, 98 + 1): for b in range(1, 99-a + 1): c = 100 - a - b Es = 0.01 * (a * 0.3862 + b * 0.3091 + c * 0.4884) aaa.append(Es) return max(aaa)

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy