简体   繁体   English

运行我的Python代码期间的语法错误

[英]Syntax error during running my Python code

Just started learning Python as beginner and I found it fun. 刚开始学习Python的初学者,我发现它很有趣。 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. 我在Google中搜索过,但找不到任何东西。 I'm using the latest version of python and I typed this in Python shell. 我正在使用最新版本的python,并在Python shell中键入了此代码。

Python is sensitive to indentation. Python对缩进很敏感。 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 . 因为您的else块与if块不对齐,请尝试不使else缩进。

fish = "basss"

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

Python differentiates between code blocks by indentation. Python通过缩进来区分代码块。 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. ifelse不得不在缩进同一水平。 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. 始终else块应与ifelif语句具有相同的缩进级别。

You have to type it on multiple lines. 您必须在多行上键入它。 The if and else statements cannot be on the same line. ifelse语句不能在同一行上。 This causes an error, and so they must be typed in separate lines. 这会导致错误,因此必须在单独的行中键入它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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