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.
CodePudding user response:
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.
CodePudding user response:
Taking into consideration that you need to create 4 separate variables. Here's what you can do:
from random import sample
a = 10
b = 18
c = 15
d = 92
for i in sample(['a','b','c','d'], k=2):
exec(f"{i} = '-'")
print(a,b,c,d)
Using sample
ensures non-repeating values.
However, this approach is not recommended but is just provided to help you understand the problem better. I recommend using a list or dictionary as stated by other fellow developers.
CodePudding user response:
Given the constraint of four named variables, I might do:
from random import sample
a = 10
b = 18
c = 15
d = 92
v = sample("abcd", 2)
if "a" in v:
a = "_"
if "b" in v:
b = "_"
if "c" in v:
c = "_"
if "d" in v:
d = "_"
print(a, b, c, d)
This is readable, but it's also extremely repetitive. It's much easier to do it if there aren't four individual variables:
from random import sample
nums = {
"a": 10,
"b": 18,
"c": 15,
"d": 92,
}
for v in sample(nums, 2):
nums[v] = "_"
print(*nums.values())
CodePudding user response:
import random
data = [10, 18, 15, 92]
already_changed = []
for i in range(2): # change 2 to however many numbers you want to change
index=random.randint(0,len(data)-1) # randomly select an index to change
while index in already_changed: # makes sure not to change the same index
index = random.randint(0, len(data) - 1)
data[index] = "_"
print(data)
CodePudding user response:
You cannot do choice(a, b, c, d) = '-'
because choice is not a variable and can therefore not be assigned.
You should store the variables in a list then replace random elements with your string. That could be done like so:
from random import randint
a = 10
b = 18
c = 15
d = 92
replacement = "--"
nums = [a, b, c, d]
while nums.count(replacement) < 2:
# create random index
index = randint(0, len(nums)-1)
# replace value at index with the replacement
nums[index] = replacement
print(*nums) # print each element in the list
However, be aware that this code doesnt change the values of a, b, c, d. If you want that, you need to reset the variables.
CodePudding user response:
You can achieve this by picking any two indices from a list of items at random, and replacing the element at that index position with _
:
import random
x = [10,18,15,92]
for i in random.sample(range(len(x)), 2):
x[i] = '_'
print(x)