I need to change the value of two random variables out of four to ‘—’. How do I do it with maximum effectiveness and readability?
Code below is crap just for reference.
from random import choice
a = 10
b = 18
c = 15
d = 92
choice(a, b, c, d) = '—'
choice(a, b, c, d) = '—'
print(a, b, c, d)
>>> 12 — — 92
>>> — 19 — 92
>>> 10 18 — —
I’ve tried choice(a, b, c, d) = ‘—’ but ofc it didn’t work. There’s probably a solution using list functions and methods but it’s complicated and almost impossible to read, so I’m searching for an easier solution.
>Solution :
Variable names are not available when you run your code, so you cannot change a "random variable". Instead, I recommend that you use a list or a dictionary. Then you can choose a random element from the list or a random key from the dictionary.