Indentation of IF-ELSE block in python

前端 未结 7 1279
青春惊慌失措
青春惊慌失措 2020-12-11 08:48

Hi I am python newbie and I am working on NLP using python. I am having a error in writing if-else block in python. When I am writing only if block at that time it is workin

7条回答
  •  孤街浪徒
    2020-12-11 09:40

    It's hard to see from your post what the problem is, but an if-else is formatted like so

     if someCondition:
         do_something       # could be a single statement, or a series of statements
     else:
         do_something_else  # could be a single statement, or a series of statements
    

    I.e., the else needs to be at the same level as the corresponding if.

    See this Python doc/tutorial on if, and this other tutorial too.

    Sometimes when your editor does autoindent for you and you edit manually too things might get messed up, so you'll have to figure out how your editor handles indentations (e.g., is it always using tabs or spaces?, what happens if you hit return etc).

    Also, be wary of mixing tabs and spaces, that will cause problems too (hard to spot since both are "invisible")

    With your updated post:

       if xyzzy.endswith('l'):
           print xyzzy
       else:
           something_else
    

提交回复
热议问题