簡體   English   中英

AttributeError:'tuple'對象沒有屬性'write'

[英]AttributeError: 'tuple' object has no attribute 'write'

我有一個Python類的作業,我遇到了一個我不理解的錯誤。 在Windows 7上運行Python IDLE v3.2.2。

以下是問題發生的地方:

#local variables
number=0
item=''
cost=''

#prompt user how many entries
number=int(input('\nHow many items to add?: '))

#open file
openfile=('test.txt','w')

#starts for loop to write new lines
for count in range(1,number+1):
    print('\nFor item #',count,'.',sep='')
    item=input('Name:  ')
    cost=float(input('Cost: $'))

    #write to file
    openfile.write(item+'\n')
    openfile.write(cost+'\n')

#Display message and closes file
print('Records written to test.txt.',sep='')
openfile.close

這是我得到的錯誤:

回溯(最近一次調用最后一次):文件“I:\\ Cent 110 \\ test.py”,第19行,在openfile.write中(item +'\\ n')
AttributeError:'tuple'對象沒有屬性'write'

你錯過了開放

openfile = open('test.txt','w')

最后,當您嘗試關閉文件時,會丟失一些parens

openfile.close()

編輯 :我剛看到另一個問題。

openfile.write(str(cost)+'\n')

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM