3. Writing


#1

For the output I get
1
4
9
16
25
36
49
64
81
100

as is expected. However, I get an error message saying: "It looks like your file doesn't contain each element from my_list on its own line."

I suspect this has to do with the way I used "\n"? I'm not sure how to use this bit.
If someone could point out my errors that would be great.

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

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

for x in my_list:
    my_file.write("%s %s" %(x, "\n"))

my_file.close()

#2

nevermind. I swapped %s for %d and it worked


#3

I think you changed something else as well, maybe you removed the trailing spaces at the end of each line

%s and %d produce the same result for ints


#4

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