如何解决“分配前引用的局部变量”问题

时间:2019-02-09 19:36:01

标签: python-3.x

我正在尝试制作一个脚本以生成数字,然后将其放入文件中 并在文件达到特定行数时停止,这是代码:

 def generating ():
       while (stop()==True):
        numb = '123456789'
        n = 12
        file  = open('in.txt','w')
        for x in itertools.product(numb , repeat = n):
         file.write("%s\n" % ''.join(map(str, x)))



def stop ():

 f = open('in.txt')
 for k, l in enumerate(f):
        pass
 k = k+1
 if (k == 3):
     return False

跟踪:

Traceback (most recent call last):
  File "C:/Users/Im Root Bicthes/Desktop/files/files.py", line 43, in <module>
    generating ()
  File "C:/Users/Im Root Bicthes/Desktop/files/files.py", line 26, in generating


     while (stop()==True):

  File "C:/Users/Im Root Bicthes/Desktop/files/files.py", line 38, in stop
    k = k+1
UnboundLocalError: local variable 'k' referenced before assignment

0 个答案:

没有答案