#๐ why is if string_1 == string_2: not working here?
40 messages ยท Page 1 of 1 (latest)
@blissful kernel
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.
your first if statement doesn't do what you think it does
you want !or
!or
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.")
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
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?
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)
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
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
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
what is the order in which a python code is run?
I skipped coding when I was at uni, worst mistake if you're doing natural sciences, it's good you're learning this stuff
im a beginner obviously. and so far from all the SST
just top down and operator prior
yeah, what happens when a function executes the return statement?
print stops it completely and return back to the return on top?
print() has no effect on whether the function continues running, return immediately stops it and nothing beyond it gets executed
look back at your original code, if two strings are the same and both contain the syllable, what do you check first? will the other check get executed?
oh right. in my notes says return will immediately stops functionn.
it will just check the first one and stops at that?
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?
are u saying it was redundant
it just returns to the first return which is true?
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
ok thanks for the tips and information. i think my biggest problem is that i dont get return function fully
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.