NameError:全局名称'变量'没有定义python

时间:2015-01-22 17:57:16

标签: python variables global-variables syntax-error

我正在处理一个项目并且此错误不断出现:

class PRNG:
    def __init__(self):
    # parameters
    # P-256 prime
    self.p=115792089210356248762697446949407573530086143415290314195533631308867097853951

    self.a=self.p-3
    self.b=41058363725152142129326129780047268409114441015993725554835256314039467401291
    self.E=curve(self.a,self.b,self.p)

    self.E.n=115792089210356248762697446949407573529996955224135760342422259061068512044369

    self.P=point(0,46263761741508638697010950048709651021688891777877937875096931459006746039284)
    self.k=183521403747637560534595403690771364941493702673414885451510208165414833985
    self.Q=mult(self.k,self.P,self.E)

    self.t=bytes_to_int(os.urandom(32)) # initial seed
    #print self.t
    self.output_length=240
    self.truncate_length=16

    def function_attack(self, nbytes, predicted_state):
        calls = ((nbytes*8-1)/self.output_length)+1
        out = ''
        for i in xrange(calls):
            tP=mult(predicted_state,self.P,self.E)
            s=tP.x
            sQ=mult(s,self.Q,self.E)
            r=sQ.x
            r_out=r % (2**self.output_length)
            self.t=s
            out = out + int_to_bytes(r_out, self.output_length/8)
        return out[:nbytes]

def function(self, nbytes):
        calls = ((nbytes*8-1)/self.output_length)+1
        out = ''
        for i in xrange(calls):
            tP=mult(self.t,self.P,self.E)
            s=tP.x
            sQ=mult(s,self.Q,self.E)
            r=sQ.x
            r_out=r % (2**self.output_length)
            self.t=s
            out = out + int_to_bytes(r_out, self.output_length/8)
        return out[:nbytes]

第一种方法是在一个单独的文件中调用,输出始终如下(无论我是否更改本地变量的名称调用):

File "C:\file1.py", line 32, in <module>
    prng = PRNG()
  File "C:\file_where_error_occurs.py", line 286, in __init__
    for i in xrange(calls):
NameError: global name 'calls' is not defined

python在做什么?

1 个答案:

答案 0 :(得分:0)

你有缩进错误......这通常是由混合制表符和空格引起的......大多数体面的编辑都可以很容易地为你解决这个问题...看你的缩进错误会使你的程序运行

python -tt my_program.py