# Count() function on array has a problem?

v4.2.1

### Question

Hi! i’m pretty new to godot, and i’m trying to learn it, but i encountered a problem in my code, that i’m not understanding. In short i’ll explain what my code should do:

So, i’m picking 1 dice for every player, after that the system throw the dices and append the results in an array and check how many occurrencies there is.
the problem is that the system does not always detect recurrences. Why?
I’ll paste the code and the output below, so you can understand better:

``````var thrown_numbers = []

if selected == 0:
# picking 3 dices, 1 for each player
for i in range(1,4):
# throwing the dice
var dice = randi_range(1,6)
# append the results to the array
thrown_numbers.append(dice)
# checking how many values are ==
var values_check = thrown_numbers.count(dice)
# if the array has 3 items, print the content of the array and the number of recurrences
if thrown_numbers.size() == 3:
print(thrown_numbers)
print(values_check)
``````

after pressing the botton a few times, here is the output:

``````[1, 2, 1]
2
[6, 1, 6]
2
[6, 3, 1]
1
[6, 2, 3]
1
[2, 2, 1]
1
``````

as you can see in the output the first result is the numbers rolled and the second is the number of recurrences(? I hope so). in the last result we can see that the result is [2,2,1], but the number of recurrences is 1. Which does not happen in the first two cases in the list, where instead I receive a 2. My question is: am i wrong with something, i’m not understanding something or is just a problem of the count() function?

Thanks in advance for your help and sorry for my English I hope you understand my problem anyway.

`values_check` is getting overwritten each time through the loop so what you’re printing at the end is the number of occurrences of the last number to be rolled. In [2,2,1] the last number to be rolled is 1 and it only occurs once in that set.

1 Like

Thank you very much for the answer… sometimes I don’t see the obvious. Thanks again!

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.