#๐ Why is there a syntax error from the first parenthises
25 messages ยท Page 1 of 1 (latest)
@twin gyro
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.
can you copy and pate the code
def straight_flush (hand:dict{tuple(int,int)}) -> bool:
"""
Check if a hand is a straight flush.
Args:
hand (list): A list of cards.
Returns:
bool: True if the hand is a straight flush, False otherwise.
hand = [(1, 0), (2, 0), (3, 0), (4, 0), (5, 0)]
>>> straight_flush(hand)
True
>>> hand = [(1, 0), (2, 0), (3, 1), (4, 0), (5, 0)]
>>> straight_flush(hand)
False
"""
hand_dict = organize_hand_by_suit(hand) # Organize the hand by suit with the previous function
for suit, cards in hand_dict.items(): # Check each suit in the hand
if len(cards) >= 5: # Check if there are at least 5 cards in the suit
cards.sort() # Sort the cards in the suit in ascending order
for i in range(len(cards) - 4):
if cards[i + 4] - cards[i] == 4: # Check if the ranks are consecutive
# If the ranks are consecutive, set the rank to 1 for straight flush
rank = 1
return True
return False
Hey @twin gyro!
You are using the wrong character instead of backticks. Use ```, not """.
this is just a section of the code
bro i dont know
For typehints (or well, generics in general), you use [ and ]. dict{...} isn't valid syntax.
so what do I need to change it to?
delete the space maybe that will fix it
it didnt fix it
You'd do dict[keytype, valuetype]. Same with tuple, you'd do tuple[type1, type2, ...]. Also, when you are annotating a dictionary, you annotate both the key type and the value type, so yeah.
is hand supposed to be a dict where the keys are integers and values are integers?
yes
you want hand: dict[int, int] then
the syntax is dict[K, V] where K is the type of keys and V is the type of values
oh ok
... Are you sure? Because your docstring shows passing list of two-element tuples, not a dict
yeah i think its a list i think im just confused i have been working on this code for too long
so what is it supposed to be
list[tuple[int,int]])
Yeah, if it's a list, then your original typehint was close. Just wrong types of brackets and dict should be list. Now it makes sense why you used tuple
Oh, also args is written to be a list in docstring, not only the examples ๐
This help channel has been closed. 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.