Q:

How to predefine a python variable

The following code work but it's little bit messy and most of IDE show an error for undefined variable => "myFile" even if the code works.
i = 0
block = False
while i < 10:
   if block == True:
      myFile.write("End of a Turn.")
   block = True
   myFile = open("path/of/my/file/"+str(i)+".txt", "w")
   myFile.write("The turn begin.")
   i += 1

What I want to do is to "pre-define" the variable before the first assignment:
#myFile = SOMETHING_THAT_DOES_NOT_RUIN_THE_FOLLOWING_CODE
myFile = None #RESOLVE
i = 0
block = False
while i < 10:
   if block == True:
      myFile.write("End of a Turn.")
   block = True
   myFile = open("path/of/my/file/"+str(i)+".txt", "w")
   myFile.write("The turn begin.")
   i += 1

To avoid some IDE comprehension problems.
Ty for help,
S.

A:

You can do it like this.
myFile = None
i = 0
block = False
while i < 10:
   if block and myFile:
       # ...

Or, probably cleaner:
for i in range(9):
    with open(str(i) + '.txt', 'w') as myFile:
        myFile.write('The turn begin. End of a turn')
with open(str(i + 1) + '.txt', 'w') as myFile:
        myFile.write('The turn begin.')

