You shouldn't use if-else as the 3 conditions are not exclusive.

For example, [3, 4, 1] should return in the 3rd condition but is also suitable in the 1st condition, so it returns nothing.

If you don't want to change your code a lot. You can use:

def minimal_three(a,b,c):
    if a < b:
        if a < c:
            return (a)
    if b < a:
        if b < c:
            return (b)
    if c < a:
        if c < b:
            return (c)
    return 'none'

For simple, you can try:

def minimal_three(a,b,c):
    return min(a, b, c)
Answer from danche on Stack Overflow
🌐
Scientech Easy
scientecheasy.com › home › blog › python nested if else statement
Python Nested If Else Statement - Scientech Easy
January 25, 2026 - However, the outer if condition is true, so Python will print the message ‘Hi’ on the console because the print statement belongs to the outer if block. ... x = 20 y = 10 z = 5 # Outer if statement. if x < y: if y > z: # Nested inner if statement. print('Hello') print('Hi') In this example, the outer if condition-1 is false, none of the statements inside the outer if block will execute. When we place an ‘if else’ statement inside another ‘if statement’ or ‘if else’ statement, it is called nested if else statement in Python.
🌐
W3Schools
w3schools.com › python › python_if_nested_if.asp
Python Nested If Statements
Use nested if statements when the inner logic is complex or depends on the outer condition. Use and when both conditions are simple and equally important. ... username = "Emil" password = "python123" is_active = True if username: if password: if is_active: print("Login successful") else: print("Account is not active") else: print("Password required") else: print("Username required") Try it Yourself »
Discussions

if statement - Nested if ... else (Python exercise) - Stack Overflow
I'm trying to build a function that checks which of a,b,c is less then returns the lesser value. def minimal_three(a,b,c): if a More on stackoverflow.com
🌐 stackoverflow.com
Coding exercise problem question (nested if statements)
The code is correct but the problem is worded poorly and the answer is not a great implementation of the solution. Essentially you need to account for three different scenarios: If a year is divisible by 4: Most likely is a leap year, but we have to check further If a year is divisible by 4 and 100: Most likely not a leap year, but we have to check further still If a year is divisible by 4, 100, and 400: Definitely is a leap year On the line you reference where it checks if a year is divisible by 100, it then uses that information to continue on to see if it is also divisible by 400, as that would produce a different result. More on reddit.com
🌐 r/learnpython
8
3
January 27, 2025
(beginner) if/elif/else exercises
Look up the “fizz buzz” problem, that’s an excellent example of if/elif/else More on reddit.com
🌐 r/learnpython
19
50
November 27, 2021
Best programming practice for nested if statements
There is no simple rule to this. The best solution is to write the code as readable as possible. Is 6 nested if-statements readable? Not so much, you should probable rethink that one. 2 is fine, it's simple, readable and does not take longer to execute. More on reddit.com
🌐 r/learnprogramming
5
1
December 29, 2021
🌐
PYnative
pynative.com › home › python exercises › python loops exercises: 40+ coding problems with solutions
40 Python Loops Coding Exercises with Solutions – PYnative
June 13, 2026 - if char in vowels: This is a highly ... inner if-else categorizes the remaining useful data. Practice Problem: Write a program to count the total number of digits in a given integer using a while loop....
🌐
Spark By {Examples}
sparkbyexamples.com › home › python › python nested if else statement examples
Python Nested if else Statement Examples - Spark By {Examples}
May 31, 2024 - Let's learn nested "if else" statements in Python and how they can be used to test multiple conditions. In Python programming, sometimes you need to check
🌐
CS-IP-Learning-Hub
csiplearninghub.com › python-if-else-conditional-statement-practice
70+ Python if else Statement Important Practice Questions - CS-IP-Learning-Hub
October 28, 2025 - #3 Update This Code Eng = int ( input (“Enter English Marks : ” )) Math = int ( input (“Enter Maths Marks : ” )) Sci = int ( input (“Enter Science Marks : ” )) SS = int ( input (“Enter Social Science Marks : ” )) if Eng > 80 and Math > 80 and Sci > 80 and SS > 80 : print(“Eligible for Science Stream”) elif Eng > 80 and Math > 50 and Sci > 50 : print(“Eligible for Commerce Stream”) else : if Eng > 80 and SS > 80 : print(“Eligible for Humanities Stream”) Reply ... I am a teacher with more than 19 years of experience in education field. You can contact me at csiplearninghub@gmail.com · Ch 1 AI Project Life Cycle Class 8 Question Answer June 30, 2026
🌐
Pythonclassroom
pythonclassroom.com › decisions-if-elif-else › nested-if
nested if | Python Classroom
Write Python coding using nested if statements. Ask the user if they are hungry. If the user replies yes, print “Goto the grocery store”. Else if the user replies no, print “Stay Home”. Else print “invalid choice”.
Top answer
1 of 6
3

You shouldn't use if-else as the 3 conditions are not exclusive.

For example, [3, 4, 1] should return in the 3rd condition but is also suitable in the 1st condition, so it returns nothing.

If you don't want to change your code a lot. You can use:

def minimal_three(a,b,c):
    if a < b:
        if a < c:
            return (a)
    if b < a:
        if b < c:
            return (b)
    if c < a:
        if c < b:
            return (c)
    return 'none'

For simple, you can try:

def minimal_three(a,b,c):
    return min(a, b, c)
2 of 6
3

why that code doesn't work:

def minimal_three(a,b,c):
    if a < b:
        if a < c:
            return (a)
        else:
            # what if a >= c and a < b ?
            return "i returned nothing"
    elif b < a:
        if b < c:
            return (b)
        else:
            # what if b >= c and a > b ?
            return "i returned nothing"
    elif c < a:
        if c < b:
            return (c)
        else:
            # what if b <= c and a < c ?
            return "i returned nothing"
    else:
        return 'none'

Alternative:

def min_of_two(a, b):
    if a > b:
        return b
    return a

def min_of_three(a, b, c):
    min_ab = min_of_two(a, b)
    min_abc = min_of_two(min_ab, c)
    return min_abc

def min_of_three_v2(a, b, c):
    min_ab = a
    if a > b:
        min_ab = b
    min_abc = min_ab
    if min_ab > c:
        min_abc = c
    return min_abc

def min_of_three_v3(a, b, c):
    min_abc = a
    if min_abc > b:
        min_abc = b
    if min_abc > c:
        min_abc = c
    return min_abc

if you really want to use nested if/else (this code is so long):

# if-elif-else is ok.
# nested if is hard to read
# if-elif-elif-elif-elif...-else is hard to read.
# hard to read == easy to have bugs, which is bad.

def min_abc_slower(a, b, c):
    if a > b:
        # a > b. This means min(a, b) == b
        if b > c:
            # b > c. This means min(c, min(a, b)) == c
            return c
        else:
            # b > c is False. This means b <= c.
            # So, min(c, min(a, b)) == b
            return b
    else:
        # a > b is False. This means a <= b.
        # So, min(a, b) = a
        if a > c:
            # a > c. This means min(c, min(a, b)) == c
            return c
        else:
            # a > c is False. This means a <= c
            # So, min(c, min(a, b)) == a
            return a
Find elsewhere
🌐
Softuni
python-book.softuni.org › chapter-04-complex-conditions-exam-problems.html
4.2. More Complex Conditions – Exam Problems · Programming Basics with Python
Before moving to the problems, let's first recall what nested conditions are: ... When the program operation depends on the value of a variable, we can do consecutive checks with multiple if-elif-else blocks: if condition1: # body elif condition2: # body elif condition3: # body else: # body
🌐
GeeksforGeeks
geeksforgeeks.org › python › nested-if-statement-in-python
Nested-if statement in Python - GeeksforGeeks
July 12, 2025 - age = 30 member = True if age > 18: if member: print("Ticket price is $12.") else: print("Ticket price is $20.") else: if member: print("Ticket price is $8.") else: print("Ticket price is $10.") ... Ticket price is $12.
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-if-else
Python If Else Statements - Conditional Statements - GeeksforGeeks
December 27, 2017 - If we need to execute a single ... logical operators such as and, or, not. ... A nested if-else statement is an if-else structure placed inside another if or else block....
🌐
edSlash
edslash.com › home › if else practice questions in python
if else practice questions in Python - edSlash
September 27, 2025 - Practice your Python skills with our list of if-else practice questions. Each question comes with a detailed solution to help you learn and improve.
🌐
Tutorjoes
tutorjoes.in › python_programming_tutorial › nested_if_statement_in_python
Nested If Statement in Python
# Nested If Statement in Python """ 3 Marks as Input Total Average Result If Pass Grade 90-100 A 80-89 B 70-79 C Else D """ m1 = int(input("Enter Mark-1 : ")) m2 = int(input("Enter Mark-2 : ")) m3 = int(input("Enter Mark-3 : ")) total = m1 + m2 + m3 average = total / 3.0 print("Total : ", total) print("Average : ", average) if m1 >= 35 and m2 >= 35 and m3 >= 35: print("Result : Pass") if average >= 90 and average <= 100: print("Grade : A") elif average >= 80 and average <= 89: print("Grade : B") elif average >= 70 and average <= 79: print("Grade : C") else: print("Grade : D") else: print("Result : Fail") print("Grade : No Grade") To download raw file Click Here
🌐
Reddit
reddit.com › r/learnpython › coding exercise problem question (nested if statements)
r/learnpython on Reddit: Coding exercise problem question (nested if statements)
January 27, 2025 -

Came across the following exercise on learnpython.com for their python basics course

Write a program that asks the user for a year and replies with either: leap year or not a leap year.

A leap year is a year that consists of 366, and not 365, days. It roughly occurs every four years. More specifically, a year is considered leap if it is either divisible by 4 but not by 100 or divisible by 400.

The answer is below
if (year % 4) == 0:
   if (year % 100) == 0:
       if (year % 400) == 0:
           print(
'leap year'
)
       else:
           print(
'not a leap year'
)
   else:
       print(
'leap year'
)
else:
   print(
'not a leap year'
)

I'm a bit confused by the second line of code that uses the modulo operator to see if the year is divisible by 100 - specifically don't you want the output to be greater than 0 (given the parameters provided by the problem/exercise) vs the equivalency line of code provides?

The solution is not accompanied with an explanation or walkthrough.

🌐
Study.com
study.com › computer science courses › computer science 113: programming in python
Quiz & Worksheet - Python Nested If Statements | Study.com
Take a quick interactive quiz on the concepts in Python Nested If Statements: Definition & Examples or print the worksheet to practice offline. These practice questions will help you master the material and retain the information.
🌐
W3Schools
w3schools.com › python › gloss_python_if_nested.asp
Python Nested If
You can have if statements inside if statements, this is called nested if statements. x = 41 if x > 10: print("Above ten,") if x > 20: print("and also above 20!") else: print("but not above 20.") Try it Yourself »
🌐
GeeksforGeeks
geeksforgeeks.org › python3-if-if-else-nested-if-if-elif-statements
Python - if , if..else, Nested if, if-elif statements - GeeksforGeeks
x = 10 y = 5 if x > 5: if y > 5: print("x is greater than 5") elif y==5: print("x is greater than 5 and y is 5") else: print("x is greater than 5 and y is less than 5") ... The structure of the above code provides conditional checks within another ...
Published   March 7, 2025
🌐
w3resource
w3resource.com › python-exercises › python-conditional-statements-and-loop-exercises.php
Python conditional statements and loops - Exercises, Practice, Solution - w3resource
Learn about Python conditional statements and loops with 44 exercises and solutions. Practice writing code to find numbers divisible by 7 and multiples of 5, convert temperatures between Celsius and Fahrenheit, guess numbers, construct patterns, count even and odd numbers, and much more.
🌐
Python Lobby
pythonlobby.com › home › nested-if statement in python programming
Nested-if statement in Python Programming - Python Lobby PythonLobby
April 27, 2021 - The above given example is the ... also use anything else at the last. If both of the conditions inside if-statements remain false, then the statement inside else: block will execute....
🌐
Python Geeks
pythongeeks.org › python geeks › learn python › python if else, if, elif, nested if else | decision making in python
Python If Else, If, Elif, Nested if else | Decision Making in Python - Python Geeks
July 22, 2021 - age=18 if(age>=18): print("You have the right to vote!") else: print("You are not eligible to vote") ... You have the right to vote! Q2. Write a function to find the greatest among the three numbers.
🌐
TechBeamers
techbeamers.com › python-if-else
Python If-Else Statements - TechBeamers
November 30, 2025 - if Logical_Expression_1 : if Logical_Expression_1.1 : if Logical_Expression_1.1.1 : Indented Block 1.1.1 else : Indented Block else : Indented Block · Go through the below Python nested if example. Execute it, modify, practice, and learn.