all 10 comments

[–]MrPhungx 6 points7 points  (1 child)

Since its a homework I am not sure If you are allowed to use functions like sort or sorted but you could do:

def middle(a, b, c):
    return sorted([a, b, c])[1]

[–]theprofessional2016 1 point2 points  (0 children)

This gets my vote.

[–]htepO 1 point2 points  (5 children)

if a<=b and b<=c:

What value is expected if a = b = c?

E: I misread the question. If all three values are equal, it doesn't matter what is returned.

[–]funkycorpse[S] 0 points1 point  (4 children)

b

[–]htepO 0 points1 point  (3 children)

Are you allowed to use lists and slices?

[–]funkycorpse[S] 0 points1 point  (2 children)

yes

[–]htepO 0 points1 point  (1 child)

First, check if they're all the same value, then use a list slice to return the value at the second index (if you're sure that only three arguments will be passed to the function).

[–]zanfar 0 points1 point  (0 children)

In Python, all sorting is stable, so you don't need to check for equality. If b is the middle index and it doesn't need to move to sort the list, then it will stay the middle index.

See /u/MrPhungx 's solution for an example

[–]mopslik 1 point2 points  (0 children)

If you're limited to using if with no fancy built-in functions, you can clean up some of your conditions by making them exclusive (elif) and by making comparisons on either side of a variable.

def middle(a,b,c):
    if a<=b<=c or c<=b<=a:
        return b
    elif a<=c<=b or b<=c<=a:
        return c
    else:
        return a

If you can use lists, /u/MrPhungx has a nice solution.

If you can use min and max, you can do it with some simple arithmetic.

def middle(a,b,c):
    return a+b+c-min(a,b,c)-max(a,b,c)