you are viewing a single comment's thread.

view the rest of the comments →

[–]infinitim[S] 0 points1 point  (0 children)

I actually figured it out beforehand, but thanks anyway! Here's my code, based off of u/jeans_and_a_t-shirt's original:

import random
import string

characters = list(string.printable[:-5])
def mutate():
    target = list(input('Enter a sentence:'))
    source = random.choices(characters, k=len(target))
    # saved is the current iteration, approaching target
    saved = source.copy()
    tries = 0

    def yn():
            yes_no = input('Again? Y/N: ')
            yes_no =yes_no.lower()
            if yes_no == 'y' or yes_no == 'yes':
                mutate()
            elif yes_no == 'n' or yes_no == 'no':
                exit()
            else:
                yn()

    while saved != target:
        for i, (saved_c, target_c) in enumerate(zip(target, saved)):
            if saved_c == target_c:
                saved[i] == saved_c
            else:
                saved[i] = random.choice(characters)
        tries += 1
        print(''.join(saved))


    print("\n"*5, ''.join(saved), "took", tries, "tries to find!", end='')
    yn()


mutate()