Error while appending in a list in python
Question:
this is for a school project and this is just a part of the code where the error exists the error comes when i try to append my second variable
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')
The error:
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'
Answers:
list.append()
returns None
because the list is altered in place.
Don’t assign the return value, there is no need to:
x=raw_input('enter staff id')
l.append(x)
this is for a school project and this is just a part of the code where the error exists the error comes when i try to append my second variable
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')
The error:
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'
list.append()
returns None
because the list is altered in place.
Don’t assign the return value, there is no need to:
x=raw_input('enter staff id')
l.append(x)