#๐Ÿ”’ Poker Function Condition has to have all variables

87 messages ยท Page 1 of 1 (latest)

lusty wasp
#
suits = {"Clubs":"C", "Spades":"S", "Diamonds":"D", "Hearts":"H"}
ranks = {"Ace of":1,
         "Two of":2,
         "Three of":3,
         "Four of":4,
         "Five of":5,
         "Six of":6,
         "Seven of":7,
         "Eight of":8,
         "Nine of":9,
         "Ten of":10,
         "Jack of":11,
         "Queen of":12,
         "King of":13}

hand = ["3D", "JD", "QD", "5D", "AD"]

def check_royal_flush(hand):
    values = [check_card[0] for check_card in hand]
    suits = [check_card[1] for check_card in hand]
    suit_counts = defaultdict(lambda:0)
    for check_suit in suits:
        suit_counts[check_suit]+=1
    if values has [10, 11, 12, 13, 1]```

I want the last line of the code block (the condition) to be true when the list 'variables' has all of these following variables: 10, 11, 12, 13, 1. How would I achieve this?

Also please note Im not sure if Python will recognise the intergers in the condition as variables from the dictionary above.
modern pecanBOT
#

@lusty wasp

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

midnight patio
#

try

#

wait

#

values is an array too?

lusty wasp
#

Im not sure waht that means

midnight patio
#

if values has [10, 11, 12, 13, 1]

lusty wasp
#

?

midnight patio
#

is values a list/array?

#

from your code

lusty wasp
#

List

#

Arent lists and arrays the same thing

midnight patio
#

yes

#

you sounded confused

#

so thats why i specified lol

#

looks like you could try using this method

past isle
#

You can simply sort the list

#

if after you sort it, the result is [1, 10, 11, 12, 13], then it's royal

#
if sorted(values) == [1, 10, 11, 12, 13]:
lusty wasp
#

ohh yeah ==

#

What about unsorted

past isle
#

What's wrong with sorted?

lusty wasp
#

Incase the order isnt that order

past isle
#

that's why you sort it

midnight patio
#

by using sorted()

#

you're not changing values

#

just how its represented

lusty wasp
#

What if its not though?

#

What if I dont want to use sorted?

past isle
#

but it will be after you sort it

#

then it's more work for yourself

midnight patio
#

!eval

values = [3, 1]
print( values )
sorted(values)
print( values )

modern pecanBOT
#

@midnight patio :white_check_mark: Your 3.12 eval job has completed with return code 0.

001 | [3, 1]
002 | [3, 1]
lusty wasp
#

Okay, but I still want to know what if I dont want to use sorted

midnight patio
#

then you create a loop

past isle
#
if 1 in values and 10 in values and 11 in values and 12 in values and 13 in values:
#

yes, or use a loop

midnight patio
#

or that lol

lusty wasp
#

Loop as in for data in dataset: ?

past isle
#
if all([i in values for i in [1, 10, 11, 12, 13]]):
#

you could use all and a list comp

midnight patio
#

only second to the simplicity of the sorted method

lusty wasp
#

Okay thanks

lusty wasp
midnight patio
#

take notes ๐Ÿ˜‰

#

i have a whole directory where i save the code from people i help

#

in case i run into a similar issue

lusty wasp
#

Good idea

lusty wasp
#

You did all(**[**i

past isle
#

yes, all accepts an iterable as its argument

#
royal_ranks = [1, 10, 11, 12, 13]
results = [i in values for i in royal_ranks]
if all(results):
#

here's the same logic but expanded so it's not just 1 line

#

results will be a list of True and False

#

!e

values = [10, 11, 5, 7, 1]
royal_ranks = [1, 10, 11, 12, 13]
results = [i in values for i in royal_ranks]
print(results)
modern pecanBOT
#

@past isle :white_check_mark: Your 3.12 eval job has completed with return code 0.

[True, True, True, False, False]
lusty wasp
#

thanks

#
suits = {"Clubs":"C", "Spades":"S", "Diamonds":"D", "Hearts":"H"}
ranks = {"Ace of":1,
         "Two of":2,
         "Three of":3,
         "Four of":4,
         "Five of":5,
         "Six of":6,
         "Seven of":7,
         "Eight of":8,
         "Nine of":9,
         "Ten of":10,
         "Jack of":11,
         "Queen of":12,
         "King of":13}

hand = ["11D", "12D", "10D", "1D", "13D"]

def check_royal_flush(hand):
    values = [check_card[0] for check_card in hand]
    suits = [check_card[-1] for check_card in hand]
    if all([item in values for item in [10, 11, 12, 13, 1]]) and len(set(suits)) == 1:
        return True
    else:
        return False```

```print(check_royal_flush(hand))```

This returns false even though it should be True.
Has it got something to do with the dictionary?
past isle
#

Also remember the example from the other day where [0] wasn't quite grabbing the proper string?

#
hand = ["11D", "12D", "10D", "1D", "13D"]
values >> ['1', '1', '1', '1', '1']
#

this is your values list, which isn't quite right

#

using prints is a great way to debug what might be wrong. Don't just blindly run code

past isle
#

yes, you're properly getting the suit

#

but [0] does not properly grab the rank

#

what part of the string represents the rank?

lusty wasp
#

The first

#

yh thats not gonna work is it

past isle
#

nope ๐Ÿ˜ฆ

#

You can use a slice though to solve this

#

if your suit is "the last character", then the rank is "everything but the last character"

#

[0:-1]

lusty wasp
#

Ight thanks

past isle
#

that's all there is to it

#

They do the same thing

#

They also use 14 for their ace and you're using 1

#

you should definitely be using 14 since ace is the highest card

lusty wasp
#

thanks

lusty wasp
#

!close

modern pecanBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.