python how to populate a list in loop
Question:
I am trying to write a program that will receive inputs, run some calculations and store them in a list before adding the elements of the list. but I keep getting the error:
wh_list = wh_list.append(wh)
AttributeError: ‘NoneType’ object has no attribute ‘append’
code:
wh_list = []
u = len(wh_list)
if u <= 1:
while True:
inp = input("Y or N:")
B = int(input("B value:"))
C = int(input("C value:"))
D = int(input("D value:"))
wh = B * C * D
wh = int(wh)
wh_list = wh_list.append(wh)
if inp == "Y":
break
else:
Ewh = sum(wh_list)
print(Ewh)
Answers:
append
changes the list and returns None
. Thus,
wh_list = wh_list.append(wh)
will
- append
wh
to wh_list
- assign
None
to wh_list
In the next iteration, it will break, as wh_list
is a list no more.
Instead, write just
wh_list.append(wh)
I am trying to write a program that will receive inputs, run some calculations and store them in a list before adding the elements of the list. but I keep getting the error:
wh_list = wh_list.append(wh)
AttributeError: ‘NoneType’ object has no attribute ‘append’
code:
wh_list = []
u = len(wh_list)
if u <= 1:
while True:
inp = input("Y or N:")
B = int(input("B value:"))
C = int(input("C value:"))
D = int(input("D value:"))
wh = B * C * D
wh = int(wh)
wh_list = wh_list.append(wh)
if inp == "Y":
break
else:
Ewh = sum(wh_list)
print(Ewh)
append
changes the list and returns None
. Thus,
wh_list = wh_list.append(wh)
will
- append
wh
towh_list
- assign
None
towh_list
In the next iteration, it will break, as wh_list
is a list no more.
Instead, write just
wh_list.append(wh)