8/9 Try It Yourself


#1

I need help here. :anguished:

 my_file = with open("text.txt","r") as my_file:
    my_file.write("Road To ~Sucess~")

Codecademy returns," Oops, try again. Your syntax doesn't look quite right. Check the Hint if you need help."

Console returns:

  File "python", line 1
    my_file = with open("text.txt","r") as my_file:
                 ^
SyntaxError: invalid syntax

Tks a lot. :smile:


#2

You're trying to read the file instead of writing.

instead of "r" as one of the parameters type in "w"`.


#3

PS : I also tried this:

with open("text.txt","r") as my_file:
    my_file.write("Road To Sucess")

and it also didn't work.


#4

This:

my_file = with open("text.txt","w") as my_file:
    my_file.write("Road To ~Sucess~")

doesn't work, too.


#5

After those two lines print my_file


#6

And this shouldn't be there because the as statement takes car of the variable name.


#7

Yay!!! :smile: :smile: :smile: It worked! Tks a lot, bandit.


#8

You're welcome. :smile:


#9

i have some problem in these page -_-
my code is :
my_file = with open("text.txt","w") as my_file:
my_file.write("Road To ~Sucess~")

print my_file()
but didn't work to
what must i do?


#10

On this line remove my_file =


#11

any idea why this code is not working ?? im getting an Invalid syntax error on line 1

with open("text.txt","w") as my_file

my_file.write("this is my first file")

print my_file

my_file.close()


#12

Did you pass or you're still stuck?


#13

FYI. My code is:
no need to close,because the with do it for us

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

for item in my_list:
    with open("text.txt","w") as my_file:
        my_file.write(str(item)+"\n")

#14

should be this:

with open("text.txt","w") as my_file:
my_file.write("Road To ~Sucess~")


#15

hello @baton134 you have to put a colon (:slight_smile: after -as my_file. that's all


#16

: colon : ok just so you know


#17

Thank you so much I was spending hours and hours trying to figure it out. and you helped me understand it. Thank you.