python如何填充循环中的列表[重复]

问题描述 投票:0回答:1

这个问题在这里已有答案:

我正在尝试编写一个程序,它将接收输入,运行一些计算并在添加列表元素之前将它们存储在列表中。但我一直收到错误:wh_list = wh_list.append(wh)AttributeError:'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)
python python-3.x
1个回答
0
投票

append更改列表并返回None。从而,

wh_list = wh_list.append(wh)

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

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

相反,只写

wh_list.append(wh)
© www.soinside.com 2019 - 2024. All rights reserved.