Error message with StringReplace "is not a valid string replacement rule"

Following an example from my lectures I obtain this error and I cannot explain why it occurs.

The istructions are the following:

alphabet = CharacterRange["A","Z"];
string = "THIS IS THE TEXT TO ENCRYPT"
rc = RandomColor[26]
code = Table[alphabet[[n]]->rc[[n]],{n,Length[alphabet]}]
newstring = StringReplace[string, code]


but sometimes (but not always) I receive this error: "StringReplace::srep: Z->[] is not a valid string replacement rule" where [] stands for a certain color.

Could you help me to understand why it happens?

• What OS? What version of Mathematica? Do you have any further information as to how you reproduce this more reliably?
– ktm
Apr 3 '20 at 16:03