+ 1
Who can help me with my code?
I wrote the code, but Sololearn thinks it's an infinite loop, and because of that my code doesn't work. =( https://code.sololearn.com/c7c2ksOOtOW8/?ref=app
2 Answers
+ 3
That's because it IS an infinite loop. If a new card is already in `cards` list, then the loop will always continue and never append another new card to the list, freezing the list length to always be < 32; hence the infinite loop. The easiest fix would be to add `sovpadenia -= 1` in the else statement, but this is unnecessarily confusing.
You can use the `not in` operator to check if a list contains an item, instead of looping over each item to check. This is one way to simplify and fix the infinite while loop:-
while len(cards) < 32:
n_card = random.choice(suits) + " " + random.choice(denominations)
if n_card not in cards:
cards.append(n_card)
+ 2
Thanks very much!