我将如何对该Python程序进行多线程处理?

时间:2018-12-15 01:29:00

标签: python multithreading

我有一个Python脚本,它会根据转出随机数字直到正确的数字放在一起来猜测一个短语。显然,这很慢,因为猜对时没有数字会保留,每个词组都是随机猜测的,正确的几率很小。

该程序可以正常工作,我可以在大约10秒内成功猜出一个两位数的短语,在30秒内成功猜到一个3位数的单词。我想知道多线程在这里是否有帮助。如果是这样,我将如何对该程序进行多线程处理?

我以前没有使用Python进行线程处理的经验,这似乎有点令人困惑。

关于我将如何做的任何想法?

代码:

import string
import random
import time



possibleCharacters = string.ascii_lowercase + string.digits + string.ascii_uppercase + """ '".,!?;:/\?<>-_=+"""

target = "guessme"
print("Phrase is 'guessme', so just enter 7 below.")
digits = int(input("How many digits in the phrase? \n] "))


attemptThis = ''.join(random.choice(possibleCharacters) for i in range(digits))
attemptNext = ''

completed = False
generation = 0
generationdisplay = 0



while completed == False:


    generationdisplay += 1

    print(attemptThis + " - " + str(generationdisplay))

    attemptNext = ''
    completed = True
    for i in range(digits):
        if attemptThis[i] != target[i]:
            completed = False
            attemptNext += random.choice(possibleCharacters)
        else:
            attemptNext += str(digits)
    generation += 1
    attemptThis = attemptNext


print("Phrase Cracked! That took " + str(generation) + " generation(s)." + "Phrase was: " + target)

如果您需要任何其他信息,我们很乐意提供。

_MouseBatteries

0 个答案:

没有答案