6

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' object has no attribute 'write'

4

1 に答える 1

12

openがありません。

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

最後に、ファイルを閉じようとすると括弧がありません

openfile.close()

編集:別の問題を見ました。

openfile.write(str(cost)+'\n')
于 2012-04-17T10:47:42.470 に答える