8/9 Try It Yourself

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:

1 Like

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

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

2 Likes

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.

1 Like

This:

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

doesn’t work, too.

1 Like

After those two lines print my_file

2 Likes

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

1 Like

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

2 Likes

You’re welcome. :smile:

1 Like

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?

1 Like

On this line remove my_file =

1 Like

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()

1 Like

Did you pass or you’re still stuck?

1 Like

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")
1 Like

should be this:

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

1 Like

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

: colon : ok just so you know

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

1 Like