#๐Ÿ”’ why is if string_1 == string_2: not working here?

40 messages ยท Page 1 of 1 (latest)

blissful kernel
#

sorry for spam question but why is if string_1 == string_2: not working here?

low heronBOT
#

@blissful kernel

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.

minor pecan
#

your first if statement doesn't do what you think it does

stable mortar
#

you want !or

minor pecan
#

!or

low heronBOT
#
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.")
minor pecan
#

also once you check the first statement and it's True, you exit the function before ever checking if the two strings are equal, so you won't get the return False you're looking for

blissful kernel
#

so based on the DeMorgan's Theorem stuff I get that not (a or b) = not a and b, but sorry i still dont get what im doing wrong

#

if syllable in (string_1, string_2):
return True

#

is it still my first if statment that's screwing it up?

minor pecan
# blissful kernel so based on the DeMorgan's Theorem stuff I get that not (a or b) = not a and b,...

if syllable in string_1 and string_2: works like if (syllable in string_1) and (string_2): where (syllable in string_1) is evaluated as one boolean statement X, (string_2) is evaluated as second boolean statement Y where a non-empty string is always True and then you do X and Y where Y is True unless string_2 is empty, you do not check whether syllable is in Y ever. Instead you could do if syllable in string_1 and syllable in string_2: which would do what you want.

if syllable in (string_1, string_2): this statement checks whether syllable is inside of the tuple (string_1, string_2), so the same as (syllable == string_1) or (syllable == string_2)

blissful kernel
#

i got it but by accident

#

just reversing the if statements of 1st and 2nd

#

i was playing around with !or for like 30minutes

#

tutor didnt use or either

minor pecan
#

I have sent the !or just so you can read the python bot message which explains the error in your code, not because or was necessary in that code, sorry for the confusion

blissful kernel
#

oh lmao!

#

i just started cosc 125. first year just taking this 1 course for my science degree. i write down psedocode to work out the logic and it worked so far. why was this one, the logic was reversed

#

i dont get that

minor pecan
minor pecan
blissful kernel
#

im a beginner obviously. and so far from all the SST

#

just top down and operator prior

minor pecan
blissful kernel
#

print stops it completely and return back to the return on top?

minor pecan
#

print() has no effect on whether the function continues running, return immediately stops it and nothing beyond it gets executed

minor pecan
blissful kernel
#

oh right. in my notes says return will immediately stops functionn.

#

it will just check the first one and stops at that?

minor pecan
#

yup, now read the assignment again, if both statements are true (same strings, containing syllable), what should happen in the function? what would the first if statement return if that's the one executing first? does that return what the assignment wants it to?

blissful kernel
#

are u saying it was redundant

#

it just returns to the first return which is true?

minor pecan
#

I am saying that in this test case you return True because the syllable check is executed first instead of returning False which is what the assignment wanted in this case

#

your tutor achieves this with and not all_same

blissful kernel
#

ok thanks for the tips and information. i think my biggest problem is that i dont get return function fully

low heronBOT
#
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.