比较三个数字?

Dav*_*ave 1 python comparison

我正在编程课程的介绍中,出于某种原因我对如何从这里开始感到有点困惑.基本上,提示是比较用户输入的三个数字,并查看第一个数字是否在最后两个数字之间.

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)
Run Code Online (Sandbox Code Playgroud)

所以你看到我无法弄清楚如何在第一个函数中得到比较函数.任何帮助深表感谢!

iCo*_*dez 7

Python允许您链接比较运算符:

if a < b < c:
Run Code Online (Sandbox Code Playgroud)

这将测试是否b介于ac独占之间.如果您想要包容性,请尝试:

if a <= b <= c:
Run Code Online (Sandbox Code Playgroud)

所以,在你的代码中,它将是这样的:

if a < read < b:
    return print("Yes")
elif b < read < a:
    return print("Yes")
else:
    return print("No")
Run Code Online (Sandbox Code Playgroud)

或者,更简洁地说:

if (a < read < b) or (b < read < a):
    return print("Yes")
else:
    return print("No")
Run Code Online (Sandbox Code Playgroud)

另请注意,print始终None在Python中返回.所以,return print("Yes")相当于return None.也许你应该删除return语句:

if (a < read < b) or (b < read < a):
    print("Yes")
else:
    print("No")
Run Code Online (Sandbox Code Playgroud)