2015-09-26 114 views
0

追加這是一個學校項目,這只是其中的錯誤存在的錯誤代碼的部分是當我嘗試添加我的第二個變量錯誤而在名單中的蟒蛇

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' 

回答

1

list.append()回報None因爲列表代替改變。

不指定返回值,沒有必要:

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

很抱歉,但仍無法得到它可以請您詳細闡述 –

+1

@SarthakKinger:刪除所有的'L ='任務;每當你這樣做時,你用'None'替換了你的列表。 –

+0

現在,謝謝你,我收到了Martijn Pieters –