#๐Ÿ”’ I need help

23 messages ยท Page 1 of 1 (latest)

mellow flare
#

Why if I write in a while True:

if var1 in var2 or var3:
  print("hello world")  
  break

even if var1 (which is given by a input) isn't in var 2 or 3 it still print.
PS: I can't send a screen bc the var and words are in italian so you coulden't understand

jagged leafBOT
#

@mellow flare

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.

visual fjord
#

!or-gotcha

jagged leafBOT
#
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.")
mellow flare
#

mhhh

visual fjord
#

you see how the syntax after in is different?

mellow flare
#

yea

visual fjord
#

because how your just checking if var3 is falsy (like an empty string, and empty list or tuple or something like that)

mellow flare
#

thanks so much I tried it and it works

#

I'm new to programming so i'm still trying to understand basic python

visual fjord
#

your initial code would read like this to the python interpreter:

if (var1 in var2) or var3:
visual fjord
mellow flare
#

i think the tutorial was wrong about that ๐Ÿ™‚

outer gulch
#

which tutorial

mellow flare
#

the one i was watching

visual fjord
#

and welcome to the wonderful world of coding ๐Ÿ™‚

mellow flare
#

ok bye i'll close the thread

#

tysm @visual fjord

#

!close

jagged leafBOT
#
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.