lundi 28 octobre 2019

'Random' python module inside loops

I am trying to create a python script that takes a string and adds random numbers in random parts of the string. For example:

str = 'PinguinsAreFun' output = 'Pi2ngu2in4sAre4F7un'

I tried to search for a module that does this very thing but I had no luck. So I thought it would be a good idea to write my own code that will get the job done... Well.. It wasn't a good idea that's for sure...

This is what I came up with:

import random

num = '0123456789'

str = 'This_Is_A_String'

while k > 15:

    k =+ 1

    rndm = random.choice(num)

    result = str[:int(rndm)] + rndm + str[int(rndm):]

print(result)

The problem is that it only works 1 time. It outputs something like: This_i7s_A_String

Since I'm defying rndm inside the while loop I would guess that on every loop its value would change but that doesn't seem to be the case. It's unlikely that the random value assigned to rndm just happened to be the same 15 times :/

So... Any ideas on what's going on?




Aucun commentaire:

Enregistrer un commentaire