vendredi 26 février 2021

My random replace-system replaces the wrong items

I want to build a program that replaces 12345 with one of each item from the shuflo list, but if the shuflo list contains a number from 1 to 5 it will replace them too.

Here is my code:

shuflo = [1, 2, 3, 4, 5]

def build(x):
    random.shuffle(shuflo)
    lock = shuflo
    lack = str(x)
    lack = lack.replace("1", str(lock[0]))
    lack = lack.replace("2", str(lock[1]))
    lack = lack.replace("3", str(lock[2]))
    lack = lack.replace("4", str(lock[3]))
    lack = lack.replace("5", str(lock[4]))
    Ponf = [lack, eval(lack)]
    return Ponf

print(build("(1)+(2)+(3)+(4)+(5)"))

The typical outcome would be:

['(3)+(1)+(2)+(3)+(5)', 14]

In the list shuflo there is only one of 3, but some of the elements get replaced twice so it doesn't work.

How can i fix this???




Aucun commentaire:

Enregistrer un commentaire