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)
Asked By: Udodi Eezy

||

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)
Answered By: Amadan
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.