5

I'm in my intro to programming class and for some reason am a bit stumped as to how to proceed from here. Basically the prompt is to compare three numbers the user inputs and see if the first number is between the last two.

def fun1(a,b,read,):
    if a < read and read > b:
        return print("Yes")
    elif b < read and read > a:
        return print("Yes")
    else:
        return print("No")

def main():
   read = input("mid: ")
   a = input("num1 ") 
   b = input("num2 ")
   fun1(read,a,b,)
   print("result:",fun1)

So as you see I can't figure out how to get the comparing function down in the first function. Any help is much appreciated!

Dave
  • 73
  • 1
  • 1
  • 4

1 Answers1

13

Python allows you to chain comparison operators:

if a < b < c:

This will test if b is between a and c exclusive. If you want inclusive, try:

if a <= b <= c:

So, in your code, it would be something like:

if a < read < b:
    return print("Yes")
elif b < read < a:
    return print("Yes")
else:
    return print("No")

or, more concisely:

if (a < read < b) or (b < read < a):
    return print("Yes")
else:
    return print("No")

Note too that print always returns None in Python. So, return print("Yes") is equivalent to return None. Perhaps you should remove the return statements:

if (a < read < b) or (b < read < a):
    print("Yes")
else:
    print("No")
  • 1
    Thanks much! In hindsight I should've been aware of this. I think I was just too brain-dead and just resorted to here. Thanks a lot! – Dave Nov 10 '14 at 00:01