count() function on array not working ?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Ombre

Hi! I’m making a Baten Kaitos combat system clone. I managed to have a function that checks if the numbers on the card are repeated and puts them in an array with append.

pair.append(number_selected[n])

However my issue is with the next function :

print (pair)
	for n in range (1,10,1) : 
		var repetition = pair.count(n) 
		print ("repetition function ", n," repeated :", repetition, " time ")
		if repetition == 2 :
			print("P A I R number :", n)
		elif repetition == 3:
			print("T R I P L E number :", n)
		elif repetition == 4:
			print("Q U A D R U P L E number :", n)

this prints :

[4, 4, 5, 5, 5, 6, 6]
repetition function 1 repeated :0 time 
repetition function 2 repeated :0 time 
repetition function 3 repeated :0 time 
repetition function 4 repeated :0 time 
repetition function 5 repeated :0 time 
repetition function 6 repeated :0 time 
repetition function 7 repeated :0 time 
repetition function 8 repeated :0 time 
repetition function 9 repeated :0 time 

it’s not working!
However, if I create an array var pair_index = [4, 4, 4, 3, 3] I get this result :

repetition function 1 repeated :0 time 
repetition function 2 repeated :0 time 
repetition function 3 repeated :2 time 
P A I R number :3
repetition function 4 repeated :3 time 
T R I P L E number :4
repetition function 5 repeated :0 time 
repetition function 6 repeated :0 time 
repetition function 7 repeated :0 time 
repetition function 8 repeated :0 time 
repetition function 9 repeated :0 time 

So the function is working correctly, but not when I append the numbers through the function append. Why and what should I do?

:bust_in_silhouette: Reply From: crossbito

Hi!

I tested your function as follows:

var pair = [4, 4, 5, 5, 5, 6, 6]
print(pair)
for n in range (1,10,1) : 
	var repetition = pair.count(n) 
	print ("repetition function ", n," repeated :", repetition, " time ")
	if repetition == 2 :
		print("P A I R number :", n)
	elif repetition == 3:
		print("T R I P L E number :", n)
	elif repetition == 4:
		print("Q U A D R U P L E number :", n)

And I received the following output:

[4, 4, 5, 5, 5, 6, 6]
repetition function 1 repeated :0 time 
repetition function 2 repeated :0 time 
repetition function 3 repeated :0 time 
repetition function 4 repeated :2 time 
P A I R number :4
repetition function 5 repeated :3 time 
T R I P L E number :5
repetition function 6 repeated :2 time 
P A I R number :6
repetition function 7 repeated :0 time 
repetition function 8 repeated :0 time 
repetition function 9 repeated :0 time

It works as intended, but if you change 4 to “4” or 4.0, the count function won’t recognize those values as 4.

I’m quite certain that you’re using the wrong data type inside the array. You should convert all the values to integers before counting them.

for example:

for i in range(pair.size()):
	var element = pair[i]
	pair[i]= int(element) ;

for n in range (1,10,1) : 
	var repetition = pair.count(n) 
	print ("repetition function ", n," repeated :", repetition, " time ")
	if repetition == 2 :
		print("P A I R number :", n)
	elif repetition == 3:
		print("T R I P L E number :", n)
	elif repetition == 4:
		print("Q U A D R U P L E number :", n)

With this the count work even with strings and floats(“4” or 4.0 for example)