Mam zadanie domowe dla klasy Python i mam błąd, którego nie rozumiem. Uruchamianie Pythona IDLE v3.2.2 Windows 7.AttributeError: 'tuple' obiekt nie ma atrybutu 'write'
Poniżej jest, gdy problem się dzieje:
#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
Jest to błąd, który otrzymuję:
Traceback (most recent call last): File "I:\Cent 110\test.py", line 19, in openfile.write(item+'\n')
AttributeError: 'tuple' object has no attribute 'write'
Należy sprawdzeniu [ 'with'' oświadczenie] (http://docs.python.org/reference/compound_stmts.html#the-with-statement) do otwierania plików - oznacza to don muszą ręcznie je zamykać i działają poprawnie nawet w wyjątkowych sytuacjach. –
@ Lattyware: Zdecydowanie sprawdzę to. Dzięki za wejście. – dhc