Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/322.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 为什么我会出现姓名错误?_Python - Fatal编程技术网

Python 为什么我会出现姓名错误?

Python 为什么我会出现姓名错误?,python,Python,我有以下代码: from crypt import crypt from itertools import product from string import ascii_letters, digits def decrypt(all_hashes, salt, charset=ascii_letters + digits + "-"): products = (product(charset, repeat=r) for r in range(8)) chain = i

我有以下代码:

from crypt import crypt
from itertools import product
from string import ascii_letters, digits

def decrypt(all_hashes, salt, charset=ascii_letters + digits + "-"):
     products = (product(charset, repeat=r) for r in range(8))
     chain = itertools.chain.from_iterable(products)
     for candidate in chain:
         hash = crypt(candidate, salt)
         if hash in all_hashes:
              yield candidate, hash
              all_hashes.remove(hash)
              if not all_hashes:
                 return

all_hashes = ['aaRrt6qwqR7xk', 'aaacT.VSMxhms' , 'aaWIa93yJI9kU',
'aakf8kFpfzD5E', 'aaMOPiDnXYTPE', 'aaz71s8a0SSbU', 'aa6SXFxZJrI7E'
'aa9hi/efJu5P.', 'aaBWpr07X4LDE', 'aaqwyFUsGMNrQ', 'aa.lUgfbPGANY'
'aaHgyDUxJGPl6', 'aaTuBoxlxtjeg', 'aaluQSsvEIrDs', 'aajuaeRAx9C9g'
'aat0FraNnWA4g', 'aaya6nAGIGcYo', 'aaya6nAGIGcYo', 'aawmOHEectP/g'
'aazpGZ/jXGDhw', 'aadc1hd1Uxlz.', 'aabx55R4tiWwQ', 'aaOhLry1KgN3.'
'aaGO0MNkEn0JA', 'aaGxcBxfr5rgM', 'aa2voaxqfsKQA', 'aahdDVXRTugPc'
'aaaLf47tEydKM', 'aawZuilJMRO.w', 'aayxG5tSZJJHc', 'aaPXxZDcwBKgo'
'aaZroUk7y0Nao', 'aaZo046pM1vmY', 'aa5Be/kKhzh.o', 'aa0lJMaclo592'
'aaY5SpAiLEJj6', 'aa..CW12pQtCE', 'aamVYXdd9MlOI', 'aajCM.48K40M.'
'aa1iXl.B1Zjb2', 'aapG.//419wZU']


all_hashes = set(all_hashes)
salt = 'aa'
for candidate, hash in decrypt(all_hashes, salt):
     print 'Found', hash, '! The original string was', candidate
当我运行它时,我得到以下回溯错误:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 3, in decrypt
NameError: global name 'itertools' is not defined
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
文件“”,第3行,在解密中
NameError:未定义全局名称“itertools”
也不知道为什么会这样


有人请解释一下,提前谢谢

它看起来不像是您导入的
itertools

from itertools import product
不算数,因为这只会将
产品
直接拉入模块的名称空间(您的模块仍然对
itertools
的其余部分一无所知。只需添加:

import itertools
在脚本顶部,该错误应该会消失,因为现在您已将
itertools
名称空间拉入模块名称空间中的
itertools
。换句话说,要访问
chain
函数,您应该使用
itertools.chain
(正如您在上面的脚本中所做的那样).

您想要:

from itertools import chain, product
并使用
产品
,或:

import itertools
并使用
itertools.chain
itertools.product

import itertools

from itertools import izip_longest

这有助于我使用
itertools
,然后能够使用
izip_longest
迭代长度不均匀的数组。

我认为您发布的代码中存在缩进错误。(导入在不应该缩进的时候缩进),当我这样做时,它表示的是相同的,但关于“产品”您可以在顶部同时使用这两个选项:
import itertools
from itertools import product
。但是,您可能希望使用1或另一个。在这种情况下,如果您只执行
import itertools
,则将
product
更改为
itertools.product