#๐Ÿ”’ if "(]" in s then output false but does true

30 messages ยท Page 1 of 1 (latest)

tribal hawk
#

class Solution(object):
def isValid(self, s):
if "()" or "([])" in s:
return True
elif "(]" in s:
return False

fair cliffBOT
#

@tribal hawk

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.

#

Hey @tribal hawk!

Please edit your message to use a code block

```py
print('Hello, world!')
```

This will result in the following:

print('Hello, world!')```
amber drum
#

"()" or "([])" in s doesn't work like you expect

agile nebula
plush spruce
#

!or-gotcha

fair cliffBOT
#
The or-gotcha

When checking if something is equal to one thing or another, you might think that this is possible:

# Incorrect...
if favorite_fruit == 'grapefruit' or 'lemon':
    print("That's a weird favorite fruit to have.")

While this makes sense in English, it may not behave the way you would expect. In Python, you should have complete instructions on both sides of the logical operator.

So, if you want to check if something is equal to one thing or another, there are two common ways:

# Like this...
if favorite_fruit == 'grapefruit' or favorite_fruit == 'lemon':
    print("That's a weird favorite fruit to have.")

# ...or like this.
if favorite_fruit in ('grapefruit', 'lemon'):
    print("That's a weird favorite fruit to have.")
plush spruce
#

@tribal hawk since in returns a boolean, you could return that directly

tribal hawk
#

this is what i did

#

class Solution(object):
def isValid(self, s):
if s in ("()", "([])", "()[]{}"):
return True
else:
return False

fair cliffBOT
#

Hey @tribal hawk!

Please edit your message to use a code block

```py
print('Hello, world!')
```

This will result in the following:

print('Hello, world!')```
tribal hawk
#

but like this idk how to make it so no matter what is in s as long as the brackets are closed it will be true

#

bc im saying only if those 3 strings are inputed then true but it will say false if its {[()]} which should be true

plush spruce
fair cliffBOT
plush spruce
#

boolean

tribal hawk
#

idk how to like make it automatic

dreamy canopy
dreamy canopy
plush spruce
tribal hawk
#

hint

dreamy canopy
plush spruce
#

is it because it's messy

#

ah ok

tribal hawk
dreamy canopy
# tribal hawk hint

the hint is: you will definetly need to scan over the characters on by one. you will also definetly need to keep track of some state while you do

tribal hawk
#

damn ok

fair cliffBOT
#
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.