#๐Ÿ”’ Why is there a syntax error from the first parenthises

25 messages ยท Page 1 of 1 (latest)

twin gyro
vagrant mossBOT
#

@twin gyro

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.

twilit nova
#

can you copy and pate the code

twin gyro
#

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

vagrant mossBOT
#

Hey @twin gyro!

Please edit your message to use a code block

You are using the wrong character instead of backticks. Use ```, not """.

twin gyro
#

this is just a section of the code

sharp yarrow
#

bro i dont know

fickle dew
# twin gyro

For typehints (or well, generics in general), you use [ and ]. dict{...} isn't valid syntax.

twin gyro
twilit nova
#

delete the space maybe that will fix it

twin gyro
#

it didnt fix it

fickle dew
# twin gyro so what do I need to change it to?

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.

neon raven
#

is hand supposed to be a dict where the keys are integers and values are integers?

twin gyro
#

yes

neon raven
#

the syntax is dict[K, V] where K is the type of keys and V is the type of values

twin gyro
#

oh ok

wintry ledge
# twin gyro yes

... Are you sure? Because your docstring shows passing list of two-element tuples, not a dict

twin gyro
neon raven
#

so what is it supposed to be

twin gyro
#

list[tuple[int,int]])

wintry ledge
#

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

wintry ledge
# twin gyro

Oh, also args is written to be a list in docstring, not only the examples ๐Ÿ˜„

vagrant mossBOT
#
Python help channel closed for inactivity

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.