0
guys plz tell me why isnt this code working
16 Antworten
+ 7
What's not working is the foul language in it!
Fix that, and we'll talk about the rest.
+ 4
Hey... I suddenly have an original and never in this thread already written idea:
Let's store the return value!!!
https://code.sololearn.com/cMBvMY2UD8MB/?ref=app
+ 3
You can use function too, but then you have to return the file from the function.
That would look like this:
def open_file():
f = open("my_life.txt", "w+")
return f
my_file = open_file()
+ 2
I don't think it would work.
You open that file within a function, but after the function ends, it will be forgotten.
Using my_file should lead to an error message.
If you just open that file outside, without the function, it should work.
+ 2
Can you copypaste the code again?
+ 1
HonFu did that!!!plz explain the rest
+ 1
ohh is it so
+ 1
You didn't correctly copy what I wrote above.
A function returns a value exactly to the point from where it was called.
But if there's no pot to catch it, it all goes to waste.
my_file = open_file()
^
pot!
+ 1
When you return something from a function, you need to store it somewhere.
my_file = ...
+ 1
*sigh*
+ 1
If a function is returning something it has to be stored too😐
0
HonFu hmm yeah it does...but then do i ve to write the open function every time i write the close function...dont u think that's too much repetition of code
0
nope bro HonFu i tried to return the file in function too.... but still i m getting the name error back
0
HonFu
def open_file():
my_file = open("my_life.txt", "w+")
return my_file
open_file()
my_file.write("life doesnt work until u do\nso stop giving a worry bout ur conditions n start working ur butt off")
my_file.close()
open_file()
print(my_file.read())
0
HonFu i didnt get wt u said bro
0
I think because you don't store in a variable when you return my_file.
Like: file = open_file()