File Input/Output Writing Bug?


#1



Exercise in question: https://www.codecademy.com/courses/python-intermediate-en-OGNHh/0/3?curriculum_id=4f89dab3d788890003000096#

Error I receive: Oops, try again. It looks like your file doesn't contain each element from my_list on its own line.

Desired Outcome: All numbers in the list should print to the output.txt file
Sometimes the code will "work" and print the numbers to the file, but then will also print a series of strings (output pasted below). When I run the code on my desktop version of python, it prints perfectly. This is the only variant of the code I can get to print anything at all while on the site.

Output file on CodeAcademy:
1
4
9
16
25
36
49
64
81
100

81
100
tr(item)
\str(item)
\str(item)
\str(item)
\str(item)
\str(item)
\str(item)


my_list = [i**2 for i in range(1,11)]

my_file = open("output.txt", "r+")

# Add your code below!
for item in my_list:
    my_file.write("%s\n" %(str(item)))

my_file.close()


#2

Try clearing the file before writing to it.

Modes for opening are described here:
https://docs.python.org/2/library/functions.html#open

The file stuff is definitely falling apart in the track..


#3

it work for me man :slight_smile: