3

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 < b:
        if a < c:
            return (a)
    elif b < a:
        if b < c:
            return (b)
    elif c < a:
        if c < b:
            return (c)
    else:
        return 'none'

So far the code runs fine until it gets to check 'c', then it doesnt return anything, nested if else statements already get really confusing to me.

9
  • 1
    You've forgotten the cases where 1/ a < b and a > c, 2/ b < a and b > c, 3/ c <a and c > b. Commented Jul 20, 2017 at 3:05
  • 1
    either a < b, or a > b, or a == b. There is no fourth possibility. So, after the if a < b, and elif b < a. the program can reach the remaining two branches only if a == b. Commented Jul 20, 2017 at 3:07
  • Practically, your function should always return variable (even if all 3 values are equal: you just return one of them); it should thus never return 'none' (or, more appropriate for Python, None). Commented Jul 20, 2017 at 3:07
  • 1
    Try writing down all six possible orders of the variables' sizes, and checking whether your code covers each possibility. Commented Jul 20, 2017 at 3:08
  • Try to make a function for min of two. Then try min(min(a, b), c)? Commented Jul 20, 2017 at 3:09

6 Answers 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)
Sign up to request clarification or add additional context in comments.

1 Comment

but there is definitley a way to build a function that finds the least with nested if else statements . i got this exercise off a website and it said you can use the nested. so if other people have solved it then it must be done without using pythons built in min() function.
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

7 Comments

Umm... You do realize that your control flow is deeply flawed from the return statements, right?
@Zizouz212 it's an explanation, this way the OP can see why his code doesn't work
@alfasin Yes, because if a is smaller than b, but bigger than c, then the function just stops... It's not exactly helpful. A helpful answer would seek to explain the behaviour using words, not using code that isn't clear.
@Zizouz212 if it's not clear to you by reading the code than it'll become clear by executing it.
@alfasin It's better. My issue becomes that in cases where the asker is trying to learn, a simple code example isn't always enough, even if the problem appears to be simplistic in our eyes. DV retracted and now +1
|
1

Use the power of if...elif...else over if...if...if or if...else...if...else. The way you have written your code, depreciates the power of elif. The correct way should be as this:

def minimal_three(a,b,c):
if a < b and a < c:
        return (a)
elif b < c:
        return (b)
else
        return (c)

This code will always return the min no matter what numbers you give.

Explaination:

In your code, the line if a < b already tells you the comparison between a and b that which one is greater. So checking for this condition again in the second if if b < a is useless. if a is not lesser than b, then obviously it is either greater than b or equal to b. So now you must just check if b is lesser than c to prove that b is smallest or even a or b both but the returned value is always minimum. I hope you get that.

Also I don't understand whyt do you want to return None. If you provide three numbers to a function to find the minimum number, it should always and always return a number. Tell me a case where you can expect a None.

Comments

-1

Easily be done using min inbuilt function:

def min_value(a,b,c):
    return (min(a,min(b,c)))

also the number of steps in your code can be reduced with some tweaking.

4 Comments

Why do you think that max has anything to do with the question ?
Yes, but we're not trying to find the maximum
sorry abt that. min,max logic is same
I'll take away my down vote, but the premise is that the question is a learning exercise to figure out if statements. While technically an answer, it's just a poor quality answer that doesn't seek to help the OP with their current problem.
-2

It works - Finding two greatest in three int:

def Biggest(a, b, c):
    if a >= b >= c:
        print a, b, 'are the biggest two'
    elif b >= c >= a:
        print b, c, 'are the biggest two'
    else:
        print c, a, 'are the biggest two'

Find greatest in three integer:

def Biggest(a, y, z):
    Max = a
    if y > Max:
        Max = y    
    if z > Max:
        Max = z
        if y > z:
            Max = y
    return Max

3 Comments

How does this have 2 up votes? You're finding the minimum, not anything else.
chained comparison is nice, but need to return min of three.
but what about the least ?
-2

You could probably get away with using a for loop to minimise lines of code! It would look something like this:

c = 3
b = 2
a = 1
list = [a,b,c]
def minimal_three(a,b,c):
    for y in list:
        n = y
    for x in list:
        if x < n:
           print(n)

4 Comments

might be a good idea if it is min of a dozen number. global variable is ugly. not sure if that function actually works.
If you really wanted to minimize code, you'd just do min(list) Also, this doesn't exactly work - your first loop just goes through the entire list, and the last one just prints when it discovers a "lower" value, but doesn't output a single, minimum value.
You should look into *args as your function definition, e.g. def minimal(n, *args): vs a global variable. You should not use list as a variable name as it hides python's builtin list type. Your first loop is unnecessary, just assign n to the first item.
oops sorry the code doesn't work it was more of a representation of what it would be like.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.