I am very new to Python (and coding in general). I am trying to create a function which creates a duplicate of a list, with any instances of the string "rand" replaced with a random integer. When the function is done, it will receive a new input list. Which is then to be appended to the same output list. We can assume there will be no repeated integers, but the "rand" string may appear many times. This is my current code. It does very close to what I want except that when the random number is already on the list, it just moves on to the next item rather than trying to create a new random number. Can anyone help me out with this? Thank you in advance.
import random
input_list = [1, 3, "rand", 2]
output_list = []
for item in my_list:
if item = "rand":
new_variable = int(random.randint(0, 4))
if new_variable in output_list:
continue
else:
output_list = item
output_list.append(new_variable)
Aucun commentaire:
Enregistrer un commentaire