8/21


#1

Hi, I'm trying to use a for loop within my "with" and "as" statement. Here is my code:

with open("text.txt", "w") as my_file:
    
    crunch = ["w","a","f","f","q","l","e"]
    
    for x in crunch:
        if x == "q":
            crunch.remove(x)
    return crunch
        
    my_file.write(crunch)

It keeps returning the same error:

  File "python", line 8
SyntaxError: 'return' outside function

Could someone please explain to me what is going wrong here?


#2

You don't need to return, as you are just editing the variables value.

Return only goes in functions.