Skip to content
Advertisement

My code is freezing and using to much CPU power. (Python)

I am attempting to make a sort of AI like script which will figure out your phrase. It is still a work in progress and is not yet complete.
Each guess should print the following info:
The text produced: ……… The string generated by the script.
The length of the text: … The length of the previously mentioned string.
The score of the text: ….. The script will score the text based on how long it is and what letters are in it.
The target text: …………….. The text it is trying to produce.
For some reason, after the first string the code freezes. I am hoping anyone can help, Thanks in advance!

import random

posAlpha = ["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", "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", "!", "?", ",", ".",
            " ", "&", "0", "1", "2", "3",
            "4", "5", "6", "7", "8", "9"]

def scoreText(input, goal):
    output = 0-abs(len(input)-len(goal))
    for i in range(len(min(input, goal, key=len))):
        if not (input[i] in goal):
            output+=0
        elif not (input[i] == goal[i]):
            output += 1
        else:
            output += 2
    if output < 1:
        return 1
    else:
        return output

goal = input("Target phrase: ")
score = 0
Ai=""
for i in range(random.randrange(round(len(goal)*0.5), len(goal)*2)):
    Ai = Ai+ random.choice(posAlpha)

score = scoreText(Ai, goal)
print("Result:        " + Ai)
print("Result Length: " + str(len(Ai)))
print("Goal:          " + goal)
print("Score:         " + str(score))
print()

while True:
    oldAi = Ai
    Ai = ""
    for i in range(len(Ai)-1):
        if (score == 1 or random.randrange(1, score) == 1):
            Ai = Ai+random.choice(posAlpha)
        else:
            Ai = Ai+oldAi[i]
    while True:
        lenVariation=random.randrange(-4, 4)
        if not (len(Ai)+lenVariation > len(goal)*2 or len(Ai)+lenVariation<round(len(goal)*0.5)):
            if lenVariation > 0:
                for i in range(lenVariation):
                    Ai = Ai+ random.choice(posAlpha)
                    break
            elif lenVariation < 0:
                for i in range(lenVariation):
                    AI=Ai[:-1]
                    break
    score = scoreText(Ai, goal)
    print("Result:        " + Ai)
    print("Result Length: " + str(len(Ai)))
    print("Goal:          " + goal)
    print("Score:         " + str(score))
    print()
input("Goal Achived!!")

Advertisement

Answer

It is because the execution is stuck in the second infinite loop. The condition (len(Ai)+lenVariation > len(goal)*2 or len(Ai)+lenVariation<round(len(goal)*0.5)) is met every time after the first execution so the if statement is never evaluated to True and the while loop is never exited.

Also, note that your break statements only exist the for loop and not the while loop so statements after the second break are never executed.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement