【发布时间】:2017-04-27 15:00:07
【问题描述】:
到目前为止,我的简单蛮力密码程序运行良好,但无论何时运行,它总是打印出最后一个并出现字符串索引超出范围错误。一切正常,我得到了我想要的输出,但是每当 while 循环关闭并且程序停止运行时,我仍然会收到错误。
userpassword = raw_input("Enter a password: ")
k = 0
cyclenumb = 0
newpasswordlist=[]
lowercaselist=["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"]
uppercaselist=["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"]
numberslist=["1", "2", "3", "4", "5", "6", "7", "8", "9", "0"]
symbolslist=["~", "`", "!", "@", "#", "$", "%", "^", "&", "*", "(", ")", "-", "_", "=", "+", "{", "[", "]", "}", "|", "\ ", ";", ":", "'", '"', ",", "<", ".", ">", "/", "?", " "]
while cyclenumb <= 10000:
for x in userpassword[k]:
for z in lowercaselist:
if x in z:
newpasswordlist.append(z)
k +=1
print newpasswordlist
if x is not z:
for x in userpassword[k]:
for z in uppercaselist:
if x in z:
newpasswordlist.append(z)
k +=1
print newpasswordlist
if x is not z:
for x in userpassword[k]:
for z in numberslist:
if x in z:
newpasswordlist.append(z)
k +=1
print newpasswordlist
if x is not z:
for x in userpassword[k]:
for z in symbolslist:
if x in z:
newpasswordlist.append(z)
k +=1
print newpasswordlist
if userpassword == newpasswordlist:
break
print newpasswordlist
print "Here is your original password " + userpassword
请帮我解决这个简单的错误。
【问题讨论】:
-
什么是“最后一个”?错误指的是哪一行?
-
第 28 行,用于用户密码 [k] 中的 x
-
但它会更改为以循环开头的其他行,例如第 35 行。我相信它会根据用户密码的最后一个字符选择哪一行来说明错误所在
标签: python for-loop indexing error-handling break