简体   繁体   中英

Syntax error during running my Python code

Just started learning Python as beginner and I found it fun. But it gave me strange errors mentioned below. I'm learning from a book and the code there is written as:

fish="basss"

if fish=="bass":
    print('super')
    else:
        print('bla')

It gave me a syntax error and I couldn't understood the reason. I have written my code as same as written in book. I've searched in google, but couldn't find anything. I'm using the latest version of python and I typed this in Python shell.

Python is sensitive to indentation. Your code should be indented like this:

fish="basss"

if fish=="bass":
    print('super')
else:
    print('bla')

because your else block isn't aligned with your if block, try un-indenting the else .

fish = "basss"

if fish == "bass":
    print('super')
else:
    print('bla')

Python differentiates between code blocks by indentation. It is very important to have correct indentation in your code.

if fish=="bass":
    print('super')
else:
    print('bla')

if and else have to be at the same level of indentation. All statements to be executed within each of those conditionals have to be indented again.

There is an indentation error , you have to do like this,

fish="basss"

if fish=="bass":
    print('super')    
else:
    print('bla')

Always else block should be under the same indentation level of if or elif statements.

You have to type it on multiple lines. The if and else statements cannot be on the same line. This causes an error, and so they must be typed in separate lines.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM