jeudi 6 avril 2017

Prevent pushing same value into array

this is my code:

number = input("hvor mange kort vil du ha?")
number2 = number -1

array = []

while(number != 0):
    if(array[number] != array[number2]):
        array.append(card())
        number -= 1
        number2 -= 1


print(array)

the commented out part is the part that doesn't work. "card" is a function that returns a random card made from two arrays 1 with colours and one with card names e.g. Q Diamonds.

Currently my code works fine so that if you ask for 7 cards then it prints out the 7 cards, but there's still the chance of 2 cards being the same out of the 54. My idea was for the if loop to check if the array value before the current appended value is not equal to each other then it will run again.

the array[number] syntax works, since I can print it outside of the loop, so I really dont understand the problem in my loop.

Aucun commentaire:

Enregistrer un commentaire