I'm following a tutorial in a textbook, "Starting out with python 2nd edition" and I'm getting a traceback with this exercise in IDLE 3.2. I can't seem to figure out the issue, it allows me to input the number of sales then only 1 sales amount it the echos "Data written to sales.txt." then displays the prompt for day 2 but any amount entered causes a traceback:
line 118, in main sales_file.write(str(sales) + '\n') ValueError: I/O operation on closed file.
Code:
def main():
num_days = int(input('For how many days do ' + \
'you have sales? '))
sales_file = open('sales.txt', 'w')
for count in range(1, num_days + 1):
sales = float(input('Enter the sales for day #' + \
str(count) + ': '))
sales_file.write(str(sales) + '\n')
sales_file.close()
print('Data written to sales.txt.')
main()
'Example ' 'long string'is the same string asExample long string.sales_file.write(…+'\n')is more simply written asprint >> sales_file, ….