3/9 error


#1

I have an error, even though the tutorial has given me the OK to go on. Nothing's been written to output.txt, it looks empty. Can someone show me what my problem is? Thanks in advance.


#2

@sheila_markazi,

myList = [ i**2 for i in range(1,11) ]
print myList
#create a file and add line's
myFile =open("output.txt", "w+")
for item in myList:
    #print item
    myFile.write(str(item) + "\n")
myFile.close()

#================
#read a file, all at once !!
f = open("output.txt","r")
#print f
line = f.read()
print line
f.close()
print "-- End of File was reached --"

#================
# Read a file, line by line
line="begin"
f = open("output.txt","r")
print "-- Begin of File --"
while line != "":
    line = f.readline()
    print line,
print "-- End of File was reached --"
f.close

= http://stackoverflow.com/questions/2967194/open-in-python-does-not-create-a-file-if-it-doesnt-exist
= https://docs.python.org/2/tutorial/inputoutput.html


3/9 Writing
#3

Sounds like in 1/9, something gets imported that we can't see?
And if you try to execute 2/9 and 3/9 without first submitting 1/9, then an error occurs. But if you run 1/9 first, then 2/9 and 3/9 write to output.txt just fine.

I don't understand what's taking place in 1/9 that makes this occur.


#4

That works for me. I've changed parametr "r+" for "w"

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

my_file = open("output.txt", "w")

Add your code below!

for i in my_list:
my_file.write(str(i) + '\n')
my_file.close()


#5

@wiwern,

Please re-edit your Post by selecting your code
then =click=ing the </> button of your editor,
and then use the indentation in your
python-code .....


#6

i'd rather made a screenshot, should be more readable
w.


#8

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

my_file = open("output.txt", "r+")
for item in my_list:
#print item
my_file.write(str(item) + "\n")
my_file.close()


#9

Hello,

You should clear "output.txt" before submitting your code.

Thank


#10

I've noticed the same thing. If I just run the code in 3/9, then I get an error saying output.txt doesn't exist (even though the autograder give me a green light to go ahead.) If I go back and run 1/9 and 2/9 and then 3/9, then no problem. I had the same problem finishing 2/9.


#11

it worked for me...