在网上找的一篇文章,不小心改了代码,运行不了了,有人会吗

######在网上找的一篇文章,不小心改了代码,运行不了了

import time
import itertools
#mylist = list(itertools.product("0123456789", repeat=10))
passwd = ("".join(x) for x in itertools.product("0123456789", repeat=10))
#print(mylist)
#print(len(mylist))
while True:
    #先直接实现,然后再添加异常
    try:
        time.sleep(0.5)
        passwd=str
        print(str)
    except StopIteration as e:
        break

只会输出无关字符

我想要:循环输出000000,000001,000002,000003,000004等

对生成器对象直接用for循环遍历:

for x in passwd:
    print(x)

也可用next方法:

import time
import itertools
#mylist = list(itertools.product("0123456789", repeat=10))
passwd = ("".join(x) for x in itertools.product("0123456789", repeat=10))
print(passwd)
#print(len(mylist))
while True:
    #先直接实现,然后再添加异常
    try:
        time.sleep(0.5)
        s=next(passwd)
        print(s)
    except StopIteration as e:
        break

你写错了,应该是

import time
import itertools
#mylist = list(itertools.product("0123456789", repeat=10))
passwd = ("".join(x) for x in itertools.product("0123456789", repeat=10))
#print(mylist)
#print(len(mylist))
while True:
   
    try:
        str = next(passwd)
        time.sleep(0.5)
        print(str)
    except StopIteration as e:
        break

测试如下

img

实现计数,为什么要用product,直接循环不行吗?


盲猜11行替换
str=passwrd