In python "else if" is spelled "elif".
Also, you need a colon after the elif and the else.

Simple answer to a simple question. I had the same problem, when I first started (in the last couple of weeks).

So your code should read:

def function(a):
    if a == '1':
        print('1a')
    elif a == '2':
        print('2a')
    else:
        print('3a')

function(input('input:'))
Answer from Frames Catherine White on Stack Overflow
🌐
W3Schools
w3schools.com › python › python_if_else.asp
Python Else Statement
Python Examples Python Compiler ... Python Certificate Python Training ... The else keyword catches anything which isn't caught by the preceding conditions....
🌐
W3Schools
w3schools.com › python › python_conditions.asp
Python If Statement
Python can evaluate many types of values as True or False in an if statement. Zero (0), empty strings (""), None, and empty collections are treated as False. Everything else is treated as True.
Discussions

python - What is the correct syntax for 'else if'? - Stack Overflow
I'm a new Python programmer who is making the leap from 2.6.4 to 3.1.1. Everything has gone fine until I tried to use the 'else if' statement. The interpreter gives me a syntax error after the 'if'... More on stackoverflow.com
🌐 stackoverflow.com
python "else" without if
This is an obscure little feature of Python known as a for-else. See https://docs.python.org/3/tutorial/controlflow.html#break-and-continue-statements-and-else-clauses-on-loops Basically, the else is run if the loop ends "naturally" without hitting a break statement. Most people consider using for-else to be more trouble then it's worth. The cleaner approach is to stick the loop inside a function and return instead of breaking: def find_divisor(num): for i in range(2, num): if num % i == 0: return i return None divisor = find_divisor(num) if divisor is None: print(num, "is a prime number") else: print(num, "is not a prime number") print(divisor, "times", num//divisor, "is", num) More on reddit.com
🌐 r/learnprogramming
23
43
February 10, 2023
How can I make sense of the `else` clause of Python loops? - Stack Overflow
Many Python programmers are probably unaware that the syntax of while loops and for loops includes an optional else: clause: for val in iterable: do_something(val) else: clean_up() The bod... More on stackoverflow.com
🌐 stackoverflow.com
Benefits of using for else syntax
Consider the following code: found = False for username in userList: if username == userInput: found = True checkPassword(username) break if not found: doSomething() I have just learnt about the for...else... syntax, what are the benefits of using for...else... syntax? More on discuss.python.org
🌐 discuss.python.org
0
0
August 26, 2023
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-if-else
Python If Else Statements - Conditional Statements - GeeksforGeeks
September 16, 2025 - In Python, If-Else is a fundamental conditional statement used for decision-making in programming.
🌐
W3Schools
w3schools.com › python › ref_keyword_else.asp
Python else Keyword
x = 5 try: x > 10 except: print("Something went wrong") else: print("The 'Try' code was executed without raising any errors!") Try it Yourself » · The if keyword. The elif keyword. Read more about conditional statements in our Python If...Else Tutorial.
🌐
Python Tips
book.pythontips.com › en › latest › for_-_else.html
21. for/else — Python Tips 0.1 documentation
That is the very basic structure of a for loop. Now let’s move on to some of the lesser known features of for loops in Python. for loops also have an else clause which most of us are unfamiliar with. The else clause executes after the loop completes normally.
🌐
Programiz
programiz.com › python-programming › if-elif-else
Python if, if...else Statement (With Examples)
number = 5 # outer if statement if number >= 0: # inner if statement if number == 0: print('Number is 0') # inner else statement else: print('Number is positive') # outer else statement else: print('Number is negative') ... Here's how this program works. ... In certain situations, the if statement can be simplified into a single line. For example, ... This one-liner approach retains the same functionality but in a more concise format. ... Python doesn't have a ternary operator.
Find elsewhere
🌐
Python documentation
docs.python.org › 3 › tutorial › controlflow.html
4. More Control Flow Tools — Python 3.14.3 documentation
When used with a loop, the else clause has more in common with the else clause of a try statement than it does with that of if statements: a try statement’s else clause runs when no exception occurs, and a loop’s else clause runs when no break occurs.
🌐
Reddit
reddit.com › r/learnprogramming › python "else" without if
r/learnprogramming on Reddit: python "else" without if
February 10, 2023 -

Hi, I've found this code

num = 407

if num == 1:
    print(num, "is not a prime number")
elif num > 1:
   # check for factors
   for i in range(2,num):
       if (num % i) == 0:
           print(num,"is not a prime number")
           print(i,"times",num//i,"is",num)
           break
   else:
       print(num,"is a prime number")
       
# if input number is less than
# or equal to 1, it is not prime
else:
   print(num,"is not a prime number")

The "else" outside the for loop doesn't match any "if" before it, but the code still works. How?

Top answer
1 of 15
220

An if statement runs its else clause if its condition evaluates to false. Identically, a while loop runs the else clause if its condition evaluates to false.

This rule matches the behavior you described:

  • In normal execution, the while loop repeatedly runs until the condition evaluates to false, and therefore naturally exiting the loop runs the else clause.
  • When you execute a break statement, you exit out of the loop without evaluating the condition, so the condition cannot evaluate to false and you never run the else clause.
  • When you execute a continue statement, you evaluate the condition again, and do exactly what you normally would at the beginning of a loop iteration. So, if the condition is true, you keep looping, but if it is false you run the else clause.
  • Other methods of exiting the loop, such as return, do not evaluate the condition and therefore do not run the else clause.

for loops behave the same way. Just consider the condition as true if the iterator has more elements, or false otherwise.

2 of 15
36

Better to think of it this way: The else block will always be executed if everything goes right in the preceding for block such that it reaches exhaustion.

Right in this context will mean no exception, no break, no return. Any statement that hijacks control from for will cause the else block to be bypassed.


A common use case is found when searching for an item in an iterable, for which the search is either called off when the item is found or a "not found" flag is raised/printed via the following else block:

Copyfor items in basket:
    if isinstance(item, Egg):
        break
else:
    print("No eggs in basket")  

A continue does not hijack control from for, so control will proceed to the else after the for is exhausted.

🌐
Real Python
realpython.com › ref › keywords › else
else | Python Keywords – Real Python
In Python, the else keyword specifies a block of code to be executed when a certain condition is false in control flow statements such as if, while, and for. It allows you to handle alternative scenarios in your code execution.
🌐
Tutorialspoint
tutorialspoint.com › python › python_if_else.htm
Python if-else Statement
The if-else statement in Python is used to execute a block of code when the condition in the if statement is true, and another block of code when the condition is false. The syntax of an if-else statement in Python is as follows − If the boolean
🌐
W3Schools
w3schools.com › python › gloss_python_for_else.asp
Python For Else
for x in range(6): if x == 3: break print(x) else: print("Finally finished!") Try it Yourself » · Python For Loops Tutorial For Loop Through a String For Break For Continue Looping Through a Range Nested Loops For pass
🌐
DigitalOcean
digitalocean.com › community › tutorials › if-else-statements-in-python
How to Use If/Else Statements in Python: A Beginner’s Guide | DigitalOcean
March 7, 2025 - Conditional statements are a fundamental part of programming, allowing code to make decisions based on certain conditions. In Python, the if/else statement helps control the execution flow by running different blocks of code depending on whether a condition is met or not.
🌐
Python documentation
docs.python.org › 3 › reference › compound_stmts.html
8. Compound statements — Python 3.14.3 documentation
This repeatedly tests the expression and, if it is true, executes the first suite; if the expression is false (which may be the first time it is tested) the suite of the else clause, if present, is executed and the loop terminates.
🌐
W3Schools
w3schools.com › python › gloss_python_else.asp
Python If Else
a = 200 b = 33 if b > a: print("b is greater than a") else: print("b is not greater than a") Try it Yourself » · Python If...Else Tutorial If statement If Indentation Elif Shorthand If Shorthand If Else If AND If OR If NOT Nested If The pass keyword in If
🌐
Python.org
discuss.python.org › python help
Benefits of using for else syntax - Python Help - Discussions on Python.org
August 26, 2023 - Consider the following code: found = False for username in userList: if username == userInput: found = True checkPassword(username) break if not found: doSomething() I have just learnt a…
🌐
Simplilearn
simplilearn.com › home › resources › software development › your ultimate python tutorial for beginners › python if-else statement: syntax and examples
If Else Statement in Python: Syntax and Examples Explained
April 16, 2025 - The if-else statement is used to execute both the true part and the false part of a given condition. If the condition is true, the if block code is executed.
🌐
DataCamp
datacamp.com › tutorial › elif-statements-python
if…elif…else in Python Tutorial | DataCamp
December 30, 2022 - if…elif…else are conditional statements used in Python that help you to automatically execute different code based on a particular condition.
🌐
Great Learning
mygreatlearning.com › blog › it/software development › else if python: understanding the nested conditional statements
Else if Python: Understanding the Nested Conditional Statements
October 14, 2024 - The "else if" statement in Python provides a way to handle multiple conditions sequentially. It allows us to specify a series of conditions to be evaluated one after another and execute the corresponding code block when a condition is true.