【问题标题】:Error while appending in a list in python [duplicate]在python中追加列表时出错[重复]
【发布时间】:2015-09-26 08:56:57
【问题描述】:

这是一个学校项目,这只是存在错误的代码的一部分,当我尝试附加我的第二个变量时出现错误

 def info():
    f=open('info.txt','a')
    ch='y'
    while ch=='y':
        l=[]
        x=raw_input('enter staff id')
        l=l.append(x)
        dob=raw_input("enter date of birth")
        l=l.append(dob)
        doj=raw_input('enter date of joining')
        l=l.append(doj)
        t=raw_input('enter duty time')
        l=l.append(t)
        sal=input('enter salary per month')
        l=l.append(sal)
        f.append(l)
        ch=raw_input('want to enter more info')

错误:

Traceback (most recent call last):
  File "<pyshell#1>", line 1, in <module>
    info()
  File "C:/Python27/staff info.py", line 9, in info
    l=l.append(dob)
AttributeError: 'NoneType' object has no attribute 'append'

【问题讨论】:

    标签: python


    【解决方案1】:

    list.append() 返回None,因为列表已就地更改。

    不赋值返回值,没必要:

    x=raw_input('enter staff id')
    l.append(x)
    

    【讨论】:

    • 对不起,还是无法得到它,请您详细说明
    • @SarthakKinger:删除所有l= 分配;每次这样做时,您都将列表替换为 None
    • 谢谢你,我明白了,Martijn Pieters
    猜你喜欢
    • 2019-04-06
    • 2020-08-08
    • 2023-03-11
    • 1970-01-01
    • 2021-11-24
    • 2017-11-30
    • 2015-11-09
    • 1970-01-01
    相关资源
    最近更新 更多