#๐Ÿ”’ if else

41 messages ยท Page 1 of 1 (latest)

dull tusk
#

without context can anyone tell me that this else is complementary to which if ?

warm marlinBOT
#

@dull tusk

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.

rotund harness
#

neither, as the indentations don't align

dull tusk
#

sorry

#

i fixed it

rotund harness
#

no, you didn't

#

does this answer your question now?

dull tusk
#

copy paste is broken

#

brb

rotund harness
#

the else is indented on the same level as if i in marked:

dull tusk
#

yes .. i wanted to make sure
in C , this would cause a dangling if else problem

#

so if k==3 : does not affect the else right

rotund harness
#

right

dull tusk
#

but theres a problem

rotund harness
#

what problem?

dull tusk
#

why is this valid too ?

#

no bad syntax warning

rotund harness
#

for...else means something else

#

the else is executed if there is no break statement executed within the for loop

#

so it runs unless you break out of the for loop

dull tusk
#

oh

#

i see

#

ohh

#

!e

for i in range(5):
    print(1)
    else:
       print(2)
print("hello")
warm marlinBOT
rotund harness
#

your else is not aligned with the for

dull tusk
# dull tusk

OHH i thought here it was for the original for

#

!e

for i in range(5):
    print(1)
else:
    print(2)
print("hello")
warm marlinBOT
dull tusk
#

i see now

#

where is this used

#

i mean how can i use this

rotund harness
#

I use it when my for loop searches for a matching item in an iterable, and doesn't find it

#

if a match is found, I break out of the loop early

dull tusk
#

wow , this is something new

#

thank you agronholm

#

!close

warm marlinBOT
#
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.