I’m trying to random some strings in a variable,
JavaScript
x
5
1
myTest = random.choice(["test1","test2","test3"])
2
print(myTest)
3
print(myTets)
4
print(myTest)
5
And when I’m running my script all of them are the same every time,
JavaScript
1
4
1
test1
2
test1
3
test1
4
I like to random my variable every time I’m calling it, like,
JavaScript
1
4
1
test1
2
test3
3
test2
4
Advertisement
Answer
This is basically the same as
JavaScript
1
5
1
x = 42
2
print(x)
3
print(x)
4
print(x)
5
A variables value only changes when you assign it:
JavaScript
1
5
1
x = 42
2
print(x)
3
x = 45
4
print(x)
5
If you want a new random value, you need to call the function again:
JavaScript
1
6
1
l = [1, 2, 3, 4, 5]
2
x = random.choice(l)
3
print(x)
4
x = random.choice(l)
5
print(x)
6