Home > Net >  Why couldn't I type multiple if-statements in a row on python?
Why couldn't I type multiple if-statements in a row on python?

Time:10-29

Why is it showing SyntaxError?
I'm totally new to learning programming, can anyone help me out with this simple question, please and thank you in advance.

x=2
if x ==2:
    print("the number is 2.")
if x%2==0:

SyntaxError: invalid syntax

Here's a link to the screenshot of the problem I'm stuck with.[link]https://postimg.cc/BjKLJ4P9

CodePudding user response:

after if you do not have statement

x=2
if x ==2:
    print("the number is 2.")
if x%2==0:
    print("the number is even.")

CodePudding user response:

Most of Python programmers use spaces rather than tabs for indentation.
Remove extra space before print. [SPACE 4 times] print("the number is 2.")

x=2
if x ==2:
  print("the number is 2.")
if x%2==0:
  print("the number is even.")

Your editor may be adding tabs but if you took a snippet of code from the web, your file may now contain both tabs & spaces.

It is easiest to go with the flow and adopt the spaces-as-indentation convention, so you will not have to reformat other people's code so much.

Adopting the spaces-as-indentation convention does not mean having to press SPACE 4 times for each indentation level. Your editor should have a configuration option which makes pressing TAB insert 4 spaces.

How To Avoid Indentation Error In Python

  • Related