为什么我无法迭代我的第一个 FOR 循环

Why I am not able to Iterate my first FOR loop

我正在编写一个 python 代码来查找具有特定规则的所有可能的密码组合

  1. 应包含字母 A-Z a-z
  2. 应包含数字 0-9
  3. 应包含特殊符号
  4. 密码首字母必须大写
from itertools import permutations

pw = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789[@_!#$%^&*()<>?/\|}{~:]"
firstchar = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"

c = permutations(pw, 2) #3 is the password length for providing sample output quickly
f=open("password.txt","w+")
f.truncate(0)

for x in firstchar:
    for i in c: 
        current_pw = x + "".join(i)
        f.write( "\t" + current_pw + "\n" )

** 输出仅包含从 A 开始的密码,并且停止不会针对 B、C 进行迭代... **

我认为 permutation 在第一个循环后退出。 所以你在每个循环中定义c

from itertools import permutations

pw = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789[@_!#$%^&*()<>?/\|}{~:]"
firstchar = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"

f=open("password.txt","w+")
f.truncate(0)

for x in firstchar:
    c = permutations(pw, 2)          # c is defined here
    for i in c: 
        current_pw = x + "".join(i)
        f.write( "\t" + current_pw + "\n" )

希望能帮到你