Reputation: 41
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)
Upvotes: 3
Views: 46195
Reputation: 82899
The other answer already explained the main problem with your code, but there is more:
aaa.append(Es)
(you did it right for the other list)aaa
is re-initialized and overwritten in each iteration of the loop; you should probably move it to the topc
; once you know a
and b
, you can calculate c
so that it satisfies the conditionb
, so the result does not exceed 100max
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)
Upvotes: 2
Reputation: 2611
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)
Upvotes: 3