我在 python 中尝试了这个密码查找器,但出了点问题

I tried this password finder in python but something went wrong

我是 python 的新手,我一直在努力学习。我尝试了这个密码查找器,它工作了 5 秒,然后开始出错。我做错了什么

import random
import time
import sys
import string

sys.setrecursionlimit(10000)

guessPass = ''

mypass = 'A87C235n'

lettersList = list(string.ascii_letters)


def SetPassword():
    global guessPass
    guessPass = ''
    for i in range(8):
        x = random.randint(0,1)
        if x == 0:
            guessPass += lettersList[random.randint(0,len(lettersList))]
        else:
            guessPass += str(random.randint(0,9))
    if (guessPass == mypass):
        print('Success i found the pass')
        print(guessPass)
    else:
        print(guessPass)
        time.sleep(0.1)
        SetPassword()

SetPassword()

这是错误:

> 1U1ix2x7
gJhzH308
Rd2657V4
97h6S0D8
40M0O066
i6C428G3
55085E95
1ggU17o1
3GCt5cv7
2SAj68lE
lSw8I3xV
667Lvwh5
x9D3u77o
677n5022
53S6Az5w
F61KqP3K
K44VP2f9
V9oaz83N
L42zM1N9
020gI0L4
0y26O0Xu
316O8aE9
JhJ183t5
929tN851
519E99uU
ybV5o021
0Osnij1J
Z9rwET0l
43H218G6
0lx733t0
8jj0323P
5rE41639
4kb7Y2HB
Nf5m9EMf
0a6OWD0D
63P5H144
m8HURuZN
896Dw57K
H43AGh2Q
i0R78H7A
fkh80236
1Ok88eF9
o1bXq7N7
4S2h174v
301Bb204
R6LC3m7N
g0Cpst1M
W93W70x8
Traceback (most recent call last):
  File "/Users/apple/Desktop/PythonEmir/69420", line 32, in <module>
    SetPassword()
  File "/Users/apple/Desktop/PythonEmir/69420", line 30, in SetPassword
    SetPassword()
  File "/Users/apple/Desktop/PythonEmir/69420", line 30, in SetPassword
    SetPassword()
  File "/Users/apple/Desktop/PythonEmir/69420", line 30, in SetPassword
    SetPassword()
  File "/Users/apple/Desktop/PythonEmir/69420", line 30, in SetPassword
    SetPassword()
  File "/Users/apple/Desktop/PythonEmir/69420", line 30, in SetPassword
    SetPassword()
  File "/Users/apple/Desktop/PythonEmir/69420", line 30, in SetPassword
    SetPassword()
  File "/Users/apple/Desktop/PythonEmir/69420", line 30, in SetPassword
    SetPassword()
  File "/Users/apple/Desktop/PythonEmir/69420", line 30, in SetPassword
    SetPassword()
  File "/Users/apple/Desktop/PythonEmir/69420", line 30, in SetPassword
    SetPassword()
  File "/Users/apple/Desktop/PythonEmir/69420", line 30, in SetPassword
    SetPassword()
  File "/Users/apple/Desktop/PythonEmir/69420", line 30, in SetPassword
    SetPassword()
  File "/Users/apple/Desktop/PythonEmir/69420", line 30, in SetPassword
    SetPassword()
  File "/Users/apple/Desktop/PythonEmir/69420", line 30, in SetPassword
    SetPassword()
  File "/Users/apple/Desktop/PythonEmir/69420", line 30, in SetPassword
    SetPassword()
  File "/Users/apple/Desktop/PythonEmir/69420", line 30, in SetPassword
    SetPassword()
  File "/Users/apple/Desktop/PythonEmir/69420", line 30, in SetPassword
    SetPassword()
  File "/Users/apple/Desktop/PythonEmir/69420", line 30, in SetPassword
    SetPassword()
  File "/Users/apple/Desktop/PythonEmir/69420", line 30, in SetPassword
    SetPassword()
  File "/Users/apple/Desktop/PythonEmir/69420", line 30, in SetPassword
    SetPassword()
  File "/Users/apple/Desktop/PythonEmir/69420", line 30, in SetPassword
    SetPassword()
  File "/Users/apple/Desktop/PythonEmir/69420", line 30, in SetPassword
    SetPassword()
  File "/Users/apple/Desktop/PythonEmir/69420", line 30, in SetPassword
    SetPassword()
  File "/Users/apple/Desktop/PythonEmir/69420", line 30, in SetPassword
    SetPassword()
  File "/Users/apple/Desktop/PythonEmir/69420", line 30, in SetPassword
    SetPassword()
  File "/Users/apple/Desktop/PythonEmir/69420", line 30, in SetPassword
    SetPassword()
  File "/Users/apple/Desktop/PythonEmir/69420", line 30, in SetPassword
    SetPassword()
  File "/Users/apple/Desktop/PythonEmir/69420", line 30, in SetPassword
    SetPassword()
  File "/Users/apple/Desktop/PythonEmir/69420", line 30, in SetPassword
    SetPassword()
  File "/Users/apple/Desktop/PythonEmir/69420", line 30, in SetPassword
    SetPassword()
  File "/Users/apple/Desktop/PythonEmir/69420", line 30, in SetPassword
    SetPassword()
  File "/Users/apple/Desktop/PythonEmir/69420", line 30, in SetPassword
    SetPassword()
  File "/Users/apple/Desktop/PythonEmir/69420", line 30, in SetPassword
    SetPassword()
  File "/Users/apple/Desktop/PythonEmir/69420", line 30, in SetPassword
    SetPassword()
  File "/Users/apple/Desktop/PythonEmir/69420", line 30, in SetPassword
    SetPassword()
  File "/Users/apple/Desktop/PythonEmir/69420", line 30, in SetPassword
    SetPassword()
  File "/Users/apple/Desktop/PythonEmir/69420", line 30, in SetPassword
    SetPassword()
  File "/Users/apple/Desktop/PythonEmir/69420", line 30, in SetPassword
    SetPassword()
  File "/Users/apple/Desktop/PythonEmir/69420", line 30, in SetPassword
    SetPassword()
  File "/Users/apple/Desktop/PythonEmir/69420", line 30, in SetPassword
    SetPassword()
  File "/Users/apple/Desktop/PythonEmir/69420", line 30, in SetPassword
    SetPassword()
  File "/Users/apple/Desktop/PythonEmir/69420", line 30, in SetPassword
    SetPassword()
  File "/Users/apple/Desktop/PythonEmir/69420", line 30, in SetPassword
    SetPassword()
  File "/Users/apple/Desktop/PythonEmir/69420", line 30, in SetPassword
    SetPassword()
  File "/Users/apple/Desktop/PythonEmir/69420", line 30, in SetPassword
    SetPassword()
  File "/Users/apple/Desktop/PythonEmir/69420", line 30, in SetPassword
    SetPassword()
  File "/Users/apple/Desktop/PythonEmir/69420", line 30, in SetPassword
    SetPassword()
  File "/Users/apple/Desktop/PythonEmir/69420", line 30, in SetPassword
    SetPassword()
  File "/Users/apple/Desktop/PythonEmir/69420", line 30, in SetPassword
    SetPassword()
  File "/Users/apple/Desktop/PythonEmir/69420", line 21, in SetPassword
    guessPass += lettersList[random.randint(0,len(lettersList))]
IndexError: list index out of range
[Finished in 5.016s]

错误来自于:

guessPass += lettersList[random.randint(0,len(lettersList))]

更正:

guessPass += lettersList[random.randint(0,len(lettersList)-1)]


解释:

lst = ['a', 'b', 'c', 'd', 'e']

print(len(lst)) # print the amount of elements in lst

for letter in lst:
    print(lst.index(letter)) # print out the index of each element

输出:

5
0
1
2
3
4

如您所见,列表中每个元素的索引从 0 开始,到列表的长度 减去一个 结束。 randint(num1, num2)将return一个介于num1num2之间的随机数,包括.