#๐Ÿ”’ uh can anyone please explain why it keeps going through the if statement even tho it's false?

28 messages ยท Page 1 of 1 (latest)

honest lark
#

can anyone please help me

abstract ferryBOT
#

@honest lark

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.

copper fjord
#

oh this is the or gotcha

#

!or

abstract ferryBOT
#
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.")
copper fjord
#

or and and have special things they do in the background that a lot of people dont know

#

in this case its not the matter but read above

honest lark
#

oh dang

#

tysm i apperciate it

copper fjord
#

int(22) will always be True

#

you could also consider using range

#

!d range

abstract ferryBOT
#

class range(stop)``````py

class range(start, stop[, step])```
The arguments to the range constructor must be integers (either built-in [`int`](https://docs.python.org/3/library/functions.html#int) or any object that implements the [`__index__()`](https://docs.python.org/3/reference/datamodel.html#object.__index__) special method). If the *step* argument is omitted, it defaults to `1`. If the *start* argument is omitted, it defaults to `0`. If *step* is zero, [`ValueError`](https://docs.python.org/3/library/exceptions.html#ValueError) is raised.

For a positive *step*, the contents of a range `r` are determined by the formula `r[i] = start + step*i` where `i >= 0` and `r[i] < stop`.

For a negative *step*, the contents of the range are still determined by the formula `r[i] = start + step*i`, but the constraints are `i >= 0` and `r[i] > stop`.
copper fjord
#

!e
print(list(range(10)))

abstract ferryBOT
honest lark
copper fjord
#

ok

honest lark
#

oh wait nvm i understand what u mean

copper fjord
#
# ...or like this.
if favorite_fruit in ('grapefruit', 'lemon'):
    print("That's a weird favorite fruit to have.")
``` this is what you'll want
honest lark
#

yes

#

tysm again

copper fjord
#

np!!!!

#

do !close or not

honest lark
#

lemme just do somethings and ill do it

copper fjord
#

ok

honest lark
#

!close

abstract ferryBOT
#
Python help channel closed with !close

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.