#๐Ÿ”’ facing this everytime i wanted to use else or elif

26 messages ยท Page 1 of 1 (latest)

queen quarry
#

i am facing this freakin syntax error everytime i want to use else or elif

meager canopyBOT
#

@queen quarry

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.

little rampart
rugged crow
#

The if and elif need to have the same indentation (number of spaces to the left of the code)

little rampart
#

yup ^

rugged crow
#
if something:
    ...
elif: something_else:
    ...
else:
    ...
#

Otherwise Python thinks it might be part of some nested if:

if ...:
    if ...:
        ...
    elif ...:
        ...
else: # relates to the very first `if` at the top, not the others!
    ...
queen quarry
#

okk

rugged crow
#

!indent

meager canopyBOT
#
Indentation

Indentation is leading whitespace (spaces and tabs) at the beginning of a line of code. In the case of Python, they are used to determine the grouping of statements.

Spaces should be preferred over tabs. To be clear, this is in reference to the character itself, not the keys on a keyboard. Your editor/IDE should be configured to insert spaces when the TAB key is pressed. The amount of spaces should be a multiple of 4, except optionally in the case of continuation lines.

Example

def foo():
    bar = 'baz'  # indented one level
    if bar == 'baz':
        print('ham')  # indented two levels
    return bar  # indented one level

The first line is not indented. The next two lines are indented to be inside of the function definition. They will only run when the function is called. The fourth line is indented to be inside the if statement, and will only run if the if statement evaluates to True. The fifth and last line is like the 2nd and 3rd and will always run when the function is called. It effectively closes the if statement above as no more lines can be inside the if statement below that line.

Indentation is used after:
1. Compound statements (eg. if, while, for, try, with, def, class, and their counterparts)
2. Continuation lines

More Info
1. Indentation style guide
2. Tabs or Spaces?
3. Official docs on indentation

rugged crow
#

More details there, sorry for the wall of text ๐Ÿ™‚

queen quarry
#

thanks guys, i am so fed up cuz i even asked chatgpt but wasnt able to understand. thanks for reacing out

#

reaching*

rugged crow
#

I'd advise turning off ChatGPT. It doesn't really know how to code, and will confidently tell you bad advice.

#

More senior devs might use AI tooling to give some direction, but you need to know which answers are accurate in order to judge it.

queen quarry
#

Any other sources that i can ask?

rugged crow
#

Ask, as in an AI? No. All generative AIs have this same issue.

#

!res Start from the basics, especially some of the YouTube creators on our list here.

meager canopyBOT
#
Resources

The Resources page on our website contains a list of hand-selected learning resources that we regularly recommend to both beginners and experts.

rugged crow
#

Free courses like Harvard's CS50, or books like Automate the Boring Stuff.

#

Of course you can always come back here with questions on things you're stuck with. ๐Ÿ™‚

queen quarry
#

Yeah

#

this server is like a god gifted one

#

i am getting instant replies just like how i got one from u

meager canopyBOT
#
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.