I'm doing a homework about a heart game with different version. It says that if we are given a list mycards that contains all the cards that player currently hold in their hands. And play is a single card that representing a potential card.And if all their cards contain either HEART(H
) or QUEEN OF SPADES(QS
) it is going to return True.
For example
>>> mycards= ['0H','8H','7H','6H','AH','QS']
>>> play = ['QS']
It will return True
this is what I have tried
if play[1] == 'H':
return True
if play == 'QS':
return True
else:
return False
But I think my codes just check one QS and one H in the list. How to make the codes that contain all
either QS or H?
Your description maps directly to the solution:
Edited for clarity:
mycards= ['0H','8H','7H','6H','AH','QS']
all((x == 'QS' or 'H' in x) for x in mycards)
# True
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With