Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How do I change 2 random variables out of 4?

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading