Saranoske
(Boris Koštel)
October 4, 2020, 10:18pm
1
I have this script:
regCOLOR = random.uniform(1, 5)
own["prop"] = regCOLOR
if regCOLOR == 1:
regalCOLOR = ('A')
if regCOLOR == 2:
regalCOLOR = ('B')
if regCOLOR == 3:
regalCOLOR = ('C')
if regCOLOR == 4:
regalCOLOR = ('D')
if regCOLOR == 5:
regalCOLOR = ('E')
The random number works but the if statement doesn’t do anything and doesn’t show any problem. It just won’t set anything to regalCOLOR.
Did I write something wrong and that’s why it doesn’t work?
TheDave
(TheDave)
October 4, 2020, 10:20pm
2
What are you hoping will happen by setting that variable?
As far as I can see, you’re simply setting a variable and then doing nothing with it.
Saranoske
(Boris Koštel)
October 4, 2020, 10:23pm
3
That one is only to test if the random variable works.
The if that should set regalCOLOR doesn’t work.
TheDave
(TheDave)
October 4, 2020, 10:24pm
4
That function returns a float, which doesn’t match any of your ‘if’ statements. You could int() it for a hacky fix, but it’ll never equal 5 so you’ll want to increase the range to 1,6.
Saranoske
(Boris Koštel)
October 4, 2020, 10:25pm
5
oh my bad, I see that’s the problem. I guess it should be random.randit.
Cotaks
(- Click for (UP)Bge resources)
October 5, 2020, 12:16am
6
indeed
own = cont.owner
regCOLOR = random.randint(1,5)
own["prop"] = regCOLOR
if regCOLOR == 1:
regalCOLOR = ('A')
elif regCOLOR == 2:
regalCOLOR = ('B')
elif regCOLOR == 3:
regalCOLOR = ('C')
elif regCOLOR == 4:
regalCOLOR = ('D')
elif regCOLOR == 5:
regalCOLOR = ('E')
print(regalCOLOR)
If you just use 1 of the outcome then an elif is better and faster.
2 Likes
edderkop
(edderkop)
October 5, 2020, 10:54am
7
this particular example can be cooked down to this.
rc = ['', 'A', 'B', 'C', 'D', 'E']
own["prop"] = random.randint(1, 5)
regalCOLOR = rc[own["prop"]]
print(regalCOLOR)
no if or elif needed, the output from the randint is predictable (range from 1 to 5) so a simple lookup would do just fine.
3 Likes
Cotaks
(- Click for (UP)Bge resources)
October 5, 2020, 12:13pm
8
i completely agree, but if you use a list then i would simply use random.choice
rc = ['A', 'B', 'C', 'D', 'E']
regalCOLOR = random.choice(rc)
print(regalCOLOR)
But it all depends on what the TS wants/ need it for.
3 Likes
Saranoske
(Boris Koštel)
October 5, 2020, 1:14pm
9
I didn’t even know you could do it like this. Thank you.
1 Like
John_tgh
(John_tgh)
October 6, 2020, 2:19am
10
@edderkop @Cotaks Your solutions are both beautiful, thanks for sharing them : D
2 Likes