Python项目中的字数不正确

时间:2015-01-26 08:48:30

标签: python

我正在为学校做这个科学展览会:http://www.sciencebuddies.org/science-fair-projects/project_ideas/CompSci_p046.shtml#summary

它们提供了一个简单的程序来“破解”您输入程序的密码。它的一种方法是使用单词列表来匹配输入的密码。我将列表升级为64216字/密码。当我说出一个词例如“割草机”。我得到了结果:

73,913次测试或每秒1,447,884次测试的搜索时间为0.05秒。

如果列表中只有64216个单词,怎么能进行73913次猜测呢?对此感到困惑

以下是该方法的代码:

def search_method_3(file_name):
    global totalguesses
    result = False

    # Start by reading the list of words into a Python list
    f = open(file_name)
    words = f.readlines()
    f.close
    # We need to know how many there are
    number_of_words = len(words)
    print("Using method 3 with "+str(number_of_words)+" in the list")

    ## Depending on the file system, there may be extra characters before
    ## or after the words. 
    for i in range(0, number_of_words):
        words[i] = cleanup(words[i])

    # Let's try each one as the password and see what happens
    starttime = time.time()
    tests = 0
    still_searching = True
    word1count = 0           # Which word we'll try next

    while still_searching:
        ourguess_pass = words[word1count]
        #print("Guessing: "+ourguess_pass)
        # Try it the way it is in the word list
        if (check_userpass(which_password, ourguess_pass)):
            print ("Success! Password "+str(which_password)+" is " + ourguess_pass)
            still_searching = False   # we can stop now - we found it!
            result = True
        #else:
            #print ("Darn. " + ourguess_pass + " is NOT the password.")
        tests = tests + 1
        totalguesses = totalguesses + 1
        # Now let's try it with the first letter capitalized
        if still_searching:
            ourguess_pass = Cap(ourguess_pass)
            #print("Guessing: "+ourguess_pass)
            if (check_userpass(which_password, ourguess_pass)):
                print ("Success! Password "+str(which_password)+" is " + ourguess_pass)
                still_searching = False   # we can stop now - we found it!
                result = True
            #else:
                #print ("Darn. " + ourguess_pass + " is NOT the password.")
            tests = tests + 1
            totalguesses = totalguesses + 1

        word1count = word1count + 1
        if (word1count >=  number_of_words):
            still_searching = False

    seconds = time.time()-starttime
    report_search_time(tests, seconds)
    return result

1 个答案:

答案 0 :(得分:1)

答案很简单:对于每个“猜测”,您可以尝试使用小写版本和大写版本。每次执行此操作时,都会增加计数。两次。这是一个清理版本,以显示我的意思:

while still_searching:
    #try lowercase password
    tests += 1
    totalguesses += 1
    if still_searching: #you can get rid of this condition because you test it in the while header already
        #try capitalized password
        tests += 1
        totalguesses += 1

因此,如果您只想查看尝试特定单词的次数而不考虑您还尝试大写版本,我建议您删除第二组增量。 也许您的意思是说您进行了x总猜测和y测试,其中x是您测试的字数,y是实际比较的数量?