python如何在循环中填充列表[duplicate]

gtlvzcf8  于 2023-03-09  发布在  Python
关注(0)|答案(1)|浏览(135)

此问题在此处已有答案

How can I collect the results of a repeated calculation in a list, dictionary etc. (make a copy of a list with each element modified)?(2个答案)
Why do these list methods (append, sort, extend, remove, clear, reverse) return None rather than the resulting list?(6个答案)
四年前关闭了。
我试图写一个程序,将接收输入,运行一些计算,并将它们存储在一个列表中,然后添加列表的元素。但我不断得到错误:wh_list = wh_list.追加(wh)属性错误:“NoneType”对象没有属性“append”
代码:

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)
0yg35tkg

0yg35tkg1#

append更改列表并返回None

wh_list = wh_list.append(wh)

意志

  • wh追加到wh_list
  • None分配给wh_list

在下一次迭代中,它将中断,因为wh_list不再是列表。
相反,只写

wh_list.append(wh)

相关问题