Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Invalid syntax on VERY SIMPLE Python if ... else statement

Can someone explain why I am getting an invalid syntax error from Python's interpretor while formulating this simple if...else statement? I don't add any tabs myself I simply type the text then press enter after typing. When I type an enter after "else:" I get the error. "Else" is highlighted by the interpreter. What's wrong?

Python 3.3.0 (v3.3.0:bd8afb90ebf2, Sep 29 2012, 10:55:48)
[MSC v.1600 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.

>>> if 3 > 0:
        print("3 greater than 0")
        else:

SyntaxError: invalid syntax
>>>
like image 981
blueuser Avatar asked Jan 14 '13 21:01

blueuser


People also ask

Which of the following is invalid Python if statement?

Answer:- if(if(a==1)){ } is an invalid if statement as we can't write if inside condition. if(a) {) is valid if a is a boolean literal else not. if((char)a) {} is invalid as there is no condition mentioned.

Is else if valid in Python?

Python if...else StatementThe if..else statement evaluates test expression and will execute the body of if only when the test condition is True . If the condition is False , the body of else is executed. Indentation is used to separate the blocks.

Why is my else statement not working?

If you are getting an error about the else it is because you've told the interpreter that the ; was the end of your if statement so when it finds the else a few lines later it starts complaining. A few examples of where not to put a semicolon: if (age < 18); if ( 9 > 10 ); if ("Yogi Bear". length < 3); if ("Jon".

What is SyntaxError in Python with example?

Syntax errors are mistakes in the use of the Python language, and are analogous to spelling or grammar mistakes in a language like English: for example, the sentence Would you some tea? does not make sense – it is missing a verb. Common Python syntax errors include: leaving out a keyword.

Why is my syntax invalid in Python?

Often, the cause of invalid syntax in Python code is a missed or mismatched closing parenthesis, bracket, or quote. These can be hard to spot in very long lines of nested parentheses or longer multi-line blocks. You can spot mismatched or missing quotes with the help of Python’s tracebacks: >>>.

How do you use if else in Python?

Python If Else is used to implement conditional execution where in if the condition evaluates to true, if-block statement (s) are executed and if the condition evaluates to false, else block statement (s) are executed. Python If-Else is an extension of Python If statement where we have an else block that executes when the condition is false.

What is if-else statement in Python?

Python If-Else is an extension of Python If statement where we have an else block that executes when the condition is false. The syntax of Python if-else statement is given below.

What happens if condition_1 is false in if-elif-else?

If condition_1 is false the program control jumps to the next elif clause and condition_2 is tested. If condition_2 is true the statements in the 1st elif block is executed. The conditions and statements in the rest of the if-elif-else structure is skipped and control comes out of the if-elif-statement to execute statements following it.


1 Answers

Python does not allow empty blocks, unlike many other languages (since it doesn't use braces to indicate a block). The pass keyword must be used any time you want to have an empty block (including in if/else statements and methods).

For example,

if 3 > 0:
    print('3 greater then 0')
else:
    pass

Or an empty method:

def doNothing():
    pass
like image 127
Isaac Dontje Lindell Avatar answered Oct 20 '22 20:10

Isaac Dontje Lindell