【问题标题】:String index out of range error Python for loops字符串索引超出范围错误 Python for 循环
【发布时间】: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


【解决方案1】:

这段代码中没有任何内容甚至尝试检测k 何时大于或等于userpassword 的长度。例如,您可以在 k 增加时检查这一点;如果太大,请跳出while 循环。

【讨论】:

    【解决方案2】:

    这种方法不是很好。你正在做for x in userpassword[k],这是没有意义的,因为userpassword[k] 总是单个字母。接下来,您将userpassword(字符串)与newpasswordlist(列表)进行比较。因此,我建议您重新定义用于此任务的算法。

    【讨论】:

      【解决方案3】:

      你可以通过这样做完全摆脱 k 的使用。我也不确定你为什么有 while 循环,但这是我对你的问题的修复。

      for x in userpassword:
          for z in lowercaselist:
              if x is z:
                  newpasswordlist.append(z)
                  print newpasswordlist
                  continue
              if x is not z:
                  for z in uppercaselist:
                      if x is z:
                          newpasswordlist.append(z)
                          print newpasswordlist
                          break
                      if x is not z:
                          for z in numberslist:
                              if x is z:
                                  newpasswordlist.append(z)
                                  print newpasswordlist
                                  break
                              if x is not z:
                                  for z in symbolslist:
                                      if x is z:
                                          newpasswordlist.append(z)
                                          print newpasswordlist
                                          break
          if userpassword is newpasswordlist:
               break
      

      【讨论】:

        猜你喜欢
        • 2021-09-02
        • 1970-01-01
        • 1970-01-01
        • 2015-05-31
        • 1970-01-01
        • 1970-01-01
        • 2015-01-19
        • 1970-01-01
        • 2013-03-09
        相关资源
        最近更新 更多