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
🌐
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 ...
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-if-else
Python If Else Statements - Conditional Statements - GeeksforGeeks
May 21, 2026 - The if statement is used to execute a block of code only when a given condition is True. If the condition is False, the code inside the if block is skipped. ... The if-else statement is used to execute one block of code when a condition is True and another block when the condition is False.
Discussions

python - What is the correct syntax for 'else if'? - Stack Overflow
Your first example can be written as print d.get(a, "3a") 2013-05-31T10:19:38.1Z+00:00 ... As you can see, else if should be changed to elif, there should be colons after '2' and else, there should be a new line after the else statement, and close the space between print and the parentheses. ... Winston C. Yang · 1,50722 gold badges1818 silver badges2727 bronze badges ... Dan D. Over a year ago · python ... More on stackoverflow.com
🌐 stackoverflow.com
if-else versus if-if ?
On July 1st, a change to Reddit's API pricing will come into effect. Several developers of commercial third-party apps have announced that this change will compel them to shut down their apps. At least one accessibility-focused non-commercial third party app will continue to be available free of charge. If you want to express your strong disagreement with the API pricing change or with Reddit's response to the backlash, you may want to consider the following options: Limiting your involvement with Reddit, or Temporarily refraining from using Reddit Cancelling your subscription of Reddit Premium as a way to voice your protest. I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns. More on reddit.com
🌐 r/learnprogramming
44
31
January 26, 2024
What does else do if I have multiple if statements
Few things to understand here: Multiple if statements are completely unrelated to each other. They will run no matter what (assuming you don't run into anything that stops execution of your code like breaks, returns, etc). else statements are connected to the if and elif statements that immediately precede it. You can have as many elif statements as you want, they should always come after one if statement, and you may have zero or one else statements after them. If you want default behavior if all of your conditions fail, you use an else statement: if condition1: pass elif condition2: pass elif condition3: pass else: print 'everything above failed' EDIT - to make sure i'm extra super dooper clear, i'll answer your questions directly: I have 3 'if' statements and one 'else'. Does the 'else' only run if none of the other 'if' statements are true or is it just dependent on the 'if' statement before it? Just dependent on the if statement before it. If it's the latter, is there an easy way of doing else for when no 'if' statements are true? You do what I did above, you make all but the first if statement into elif statements. More on reddit.com
🌐 r/learnpython
10
11
April 5, 2018
Help with text based Adventure game
This is an excellent challenge for learning to programme in Python. It is also a good time to step away from the keyboard and do some up-front design work, especially around data structures (how you want to represent your game world). A good data structure will greatly simplify your coding as you will be able to generalise things rather than having to explicitly code every step, the trap you are already falling into. For example, you might decide that every location in your game can have only 4 possible exits. An exit would lead to another location. A dict (dictionary) would be a good example of a suitable data structure. For example, locations = { 'entrance': {"north": 'dining hall', "east": 'kitchen', "south": 'EXIT', "west": None, } # end of movement direction definitions ... # another location / room } # end of definition of locations Now, you can code for any current location, e.g. 'entrance', and when the user enters one of the compass directions, you can check if that is a valid direction to head in, so in this case, "west" would not be allowed. When you are a little more advanced, you are likely going to want to look into using a class for a location, and many other data structures, as you can combine data structures and behaviours. More on reddit.com
🌐 r/learnpython
11
2
August 30, 2025
🌐
W3Schools
w3schools.com › python › python_if_else.asp
Python Else Statement
In this example a is greater than b, so the first condition is not true, also the elif condition is not true, so we go to the else condition and print to screen that "a is greater than b". ... a = 200 b = 33 if b > a: print("b is greater than a") else: print("b is not greater than a") Try it Yourself » · This creates a simple two-way choice: if the condition is true, execute one block; otherwise, execute the else block. The else statement provides a default action when none of the previous conditions are true.
🌐
freeCodeCamp
freecodecamp.org › news › python-else-if-statement-example
Python Else-If Statement Example
July 1, 2022 - If it is False, Python will move to other elif blocks if there are any present. Finally, if all of the conditions are False, then and only then the code in the else block will run. The else block essentially means that "when all else fails, run this code instead". Let’s see an example of how the elif statement works.
🌐
freeCodeCamp
freecodecamp.org › news › else-if-in-python-python-if-statement-example-syntax
Else-If in Python – Python If Statement Example Syntax
March 22, 2022 - Python provides many conditional statements for decision making, and if-else is one of them. ... The default order of execution of statements and how we can alter it. What is the if-else statement and its syntax. How to deal with multiple conditions using elif. A practical example of if else where we will write a program to check if the number is even or odd.
🌐
Tutorialspoint
tutorialspoint.com › python › python_if_else.htm
Python if-else Statement
Now, let's see the Python implementation the above flowchart. age=25 print ("age: ", age) if age >=18: print ("eligible to vote") else: print ("not eligible to vote") On executing this code, you will get the following output − ... To test ...
Find elsewhere
🌐
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.
🌐
Berkeley
pythonnumericalmethods.studentorg.berkeley.edu › notebooks › chapter04.01-If-Else-Statements.html
If-Else Statements — Python Numerical Methods
EXAMPLE: What will be the value of y after the following code is executed? x = 3 if x > 1 and x < 2: y = 2 elif x > 2 and x < 4: y = 4 else: y = 0 print(y) ... Note that if you want the logical statement a < x < b, this is two conditional statements, a < x and x < b. In Python, you can type ...
🌐
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 - If the if statement is true, the elif and else blocks will be skipped. If the if statement is false, the elif block will be executed. If the elif statement is false, the else block will be executed. Here’s an example of how to use if, elif, and else in Python:
🌐
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
2 weeks ago - 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.
Address   5851 Legacy Circle, 6th Floor, Plano, TX 75024 United States
🌐
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 - Now let's modify the example where none of the conditions evaluate to True: x = 3 if x < 2: print("x is less than 2") elif x > 5: print("x is greater than 5") else: print("x is between 2 and 5") In this case, both the conditions x < 2 and x ...
🌐
Dataquest
dataquest.io › home › blog › tutorial: using if statements in python
How to Use IF Statements in Python (if, else, elif, and more) – Dataquest
November 24, 2024 - # else statement x = 3 y = 10 if x > y: print("x is greater than y.") else: print("x is smaller than y.") ... Output: x is smaller than y. Here, Python first executes the if condition and checks if it’s True.
🌐
W3Schools
w3schools.com › python › gloss_python_else.asp
Python If Else
In this example a is greater than b, so the first condition is not true, also the elif condition is not true, so we go to the else condition and print to screen that "a is greater than b". ... 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 ...
🌐
Mimo
mimo.org › glossary › python › if-statement
Python If Statement: Master Conditional Logic | Learn Coding
Therefore, if statements can make functions more dynamic and versatile. ... def check_temperature(temp): if temp > 30: return "It's a hot day." elif temp > 20: return "It's a nice day." else: return "It's cold." print(check_temperature(25)) # "It's a nice day." In Python, some non-boolean values evaluate to True or False, too. Such "truthy" or "falsy" values can be useful in a boolean context. For example, empty sequences and collections evaluate to False, while non-empty ones evaluate to True.
🌐
The New Stack
thenewstack.io › home › how to use python if-else statements
How to Use Python If-Else Statements - The New Stack
December 21, 2023 - If-else statements should be considered a must-know to be a successful Python programmer. Essentially, the if-else statement is used to execute the true and false portion of any given condition. Continuing with my example above, the true portion could be “I will take an umbrella” and the false portion could be “If it’s not raining.” In other words, if it’s raining I will take my umbrella else (if it’s not raining) I won’t.” Now, how do you use if-else statements in Python?
🌐
freeCodeCamp
freecodecamp.org › news › python-if-else-statement-example
Python If-Else Statement Example
April 10, 2022 - With that out of the way, let's talk a bit more about conditional logic, and why if-else statements are so important to Python and other programming languages. If-else statements are a form of conditional logic. Essentially, what that means is · We test a condition. For example, whether a given variable equals another given variable.
🌐
ScholarHat
scholarhat.com › home
If Else Statement in Python Example
September 11, 2025 - However, if we want to do anything ... code block to execute if condition is False · current_hour = 15 if current_hour < 12: print("Good morning!") else: print("Good afternoon!") ... Good afternoon!...
🌐
DataCamp
datacamp.com › tutorial › elif-statements-python
elif Statements in Python: A Guide to Conditional Logic | DataCamp
June 25, 2020 - Conditional statements are one of the first real building blocks of programming logic in Python. ... if-elif-else lets you check multiple conditions in sequence, executing the block tied to the first one that's true.
🌐
InterServer
interserver.net › home › python › mastering conditional statements in python: a guide to if, else, and elif
Mastering Conditional Statements in Python: A Guide to If, Else, and Elif - Interserver Tips
April 24, 2026 - Here’s how an if-else statement looks in Python: number = 5 if number % 2 == 0: print("The number is even.") else: print("The number is odd.") In this example, the program checks whether the number is even using the modulus operator (%).