[Python] When I run my python script it shows a blank screen and disappears quickly

Discussão em 'Python' iniciado por Stack, Dezembro 20, 2024.

  1. Stack

    Stack Membro Participativo

    When I run my .py file, it quickly shows a blank black screen then disappears and does nothing. The script works fine in the editor though! The full code is down below, I know it could use some improvement but I'm just looking for answers to the blank screen for now. :) (The script is a simple genetic algorithm btw)

    #!/usr/bin/python3
    from fuzzywuzzy import fuzz
    import random
    import string

    def error_msg():
    print('\nSomethine went wrong!\nMake sure you typed the information correctly!')
    mainF()

    def mainF():
    try:
    while 7 == 7:
    print()
    stri = input('Enter string here: ')
    gene = input('Enter number of generations here: ')
    agen = input('Enter number of agents here: ')
    muta = input('Enter chance of mutation here (0 - 1): ')
    thre = input('Enter threshold here: ')
    if stri == '' or gene == '' or agen == '' or thre == '' or muta == '' or stri.isdigit() or gene.isalpha() or agen.isalpha() or thre.isalpha() or muta.isalpha():
    print('\nSomethine went wrong!\nMake sure you typed the information correctly!')
    else:
    ga(stri, len(stri), agen, gene, thre, muta)
    except:
    error_msg()

    class Agent:
    def __init__(self, length):
    self.string = ''.join(random.choice(string.ascii_letters) for _ in range(length))
    self.fitness = -1
    def __str__(self):
    return 'String: ' + str(self.string) + ', Fitness: ' + str(self.fitness) + '.'

    def init_agents(population_p, length):
    try:
    population = int(population_p)
    return [Agent(length) for _ in range(population)]
    except:
    error_msg()

    def fitness(agents, in_str_p):
    try:
    in_str = in_str_p
    for agent in agents:
    agent.fitness = fuzz.ratio(agent.string, in_str)
    return agents
    except:
    error_msg()

    def selection(agents):
    try:
    agents = sorted(agents, key=lambda agent: agent.fitness, reverse=True)
    print('\n'.join(map(str, agents)))
    agents = agents[:int(0.2 * len(agents))]
    return agents
    except:
    error_msg()

    def crossover(agents, in_str_len_p, population_p):
    try:
    population = int(population_p)
    in_str_len = in_str_len_p
    offspring = []
    for _ in range((population - len(agents)) // 2):
    parent1 = random.choice(agents)
    parent2 = random.choice(agents)
    child1 = Agent(in_str_len)
    child2 = Agent(in_str_len)
    split = random.randint(0, in_str_len)
    child1.string = parent1.string[0:split] + parent2.string[split:in_str_len]
    child2.string = parent2.string[0:split] + parent1.string[split:in_str_len]
    offspring.append(child1)
    offspring.append(child2)
    agents.extend(offspring)
    return agents
    except:
    error_msg()

    def mutation(agents, in_str_len_p, mutation_chance_p):
    try:
    mutation_chance = float(mutation_chance_p)
    in_str_len = in_str_len_p
    for agent in agents:
    for idx, param in enumerate(agent.string):
    if random.uniform(0.0, 1.0) <= mutation_chance:
    agent.string = agent.string[0:idx] + random.choice(string.ascii_letters) + agent.string[idx+1:in_str_len]
    return agents
    except:
    error_msg()

    def ga(in_str_p, in_str_len_p, population_p, generations_p, threshold_p, mutation_chance_p):
    mutation_chance = mutation_chance_p
    threshold = int(threshold_p)
    population = population_p
    generations = int(generations_p)
    in_str = in_str_p
    in_str_len = in_str_len_p
    agents = init_agents(population, in_str_len)
    for generation in range(generations):
    print('Generation: ' + str(generation))
    agents = fitness(agents, in_str)
    agents = selection(agents)
    agents = crossover(agents, in_str_len, population)
    agents = mutation(agents, in_str_len, mutation_chance)
    if any(agent.fitness >= threshold for agent in agents):
    print('Threshold met!')
    mainF()

    if __name__ == '__main__':
    mainF()

    Continue reading...

Compartilhe esta Página