Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/351.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python项目中的字数不正确_Python - Fatal编程技术网

Python项目中的字数不正确

Python项目中的字数不正确,python,Python,我正在为学校举办这个科学展: 他们提供了一个简单的程序来“破解”你输入程序的密码。一种方法是使用一个单词列表来匹配输入的密码。我将列表升级为64216个单词/密码。当我输入一个单词例如“割草机”时。我得到的结果是: 搜索花费了0.05秒进行73913次测试,或每秒1447884次测试 当列表中只有64216个单词时,怎么可能需要73913次猜测呢?对此感到困惑 下面是该方法的代码: def search_method_3(file_name): global totalguesses

我正在为学校举办这个科学展:

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

搜索花费了0.05秒进行73913次测试,或每秒1447884次测试

当列表中只有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

答案很简单:对于每一个“猜测”,您都会尝试小写版本和大写版本。每次这样做时,您都会增加计数。两次。下面是一个经过清理的版本,以说明我的意思:

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
是实际比较的次数?

我只是快速看了一眼,但你似乎在每个循环中增加
测试和
总猜测两次。而且,没有行可以打印该消息。也许代码不完整?@DavidMorenoGarcía我相信这就是
report\u search\u time
call所做的。打印该消息的行数较低,但它是主要功能的一部分,而不是这一部分。我只是想确保这不是代码中的错误。