2014-11-06 75 views
1

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

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) 

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

+0

'如果a <读取 b'。 – 2014-11-06 17:00:47

回答

4

Python允许你chain comparison operators

if a < b < c: 

这是检验bac独家之间。如果你想包,请尝试:

if a <= b <= c: 

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

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

,或者更简洁:

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

还要注意print总是在Python中返回None。所以,return print("Yes")相当于return None。也许你应该删除返回语句:

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

非常感谢!事后看来,我应该知道这一点。我想我只是脑子死了,只是在这里。非常感谢! – Dave 2014-11-10 00:01:58