3/12 File Input/Output


#1



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


When run it produces this error.
Oops, try again. It looks like your file doesn't contain each element from my_list on its own line.


Expected this code to execute, but the output.txt is empty.


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

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

# Add your code below!
for i in my_list:
    my_file.write(str(i)+"\n")
my_file.close()


#2

Turns out the bit the code academy people put in is wrong.
Change
my_file = open("output.txt", "r+")
to
my_file = open("output.txt", "w+")


#3

Try setting i to a string before you place it in the write function.


#4

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.