2014-12-06 108 views
1

我不相信我把它设置正确......因为不管我为foo()填写什么数字,它似乎总是返回“True”。我究竟做错了什么??Python Return True或False

# Complete the following function. 
# Returns True if x * y/z is odd, False otherwise. 

def foo(x, y, z): 
    answer = True 
    product = (x * y)/z 
    if (product%2) == 0: 
     answer = False 
    return answer 

print(foo(1,2,3))  
+0

让我问你,你会输入什么来返回假 – 2014-12-06 08:55:07

+0

是的,它在我的实际程序中是正确的,但我没有在这里设置正确的发布。 – 2014-12-06 08:55:45

+0

[嗯,它工作正常](http://labs.codecademy.com/CdIh#:workspace) – 2014-12-06 08:57:30

回答

6

看来,OP是困惑,因为Python 3不做整数除法使用/运算符时。

考虑对OP程序进行以下修改,以便我们可以更好地了解这一点。

def foo(x, y, z): 
    answer = True 
    product = (x * y)/z 
    print(product) 
    if (product%2) == 0: 
     answer = False 
    return answer 

print(foo(1,2,3)) 
print(foo(2,2,2)) 

Python 2中的输出:

python TrueMe.py 
0 
False 
2 
False 

Python 3中的输出:

python3 TrueMe.py 
0.6666666666666666 
True 
2.0 
False 

不用说,输入2,2,2并实际上导致产生的False返回值。

如果你想在Python3中得到整数除法,你必须使用//而不是/

+0

这对我来说是一个新闻!!!!!!!!! Thanx很多 – vks 2014-12-06 09:04:59

+1

呃...这些不同的版本正在杀死我......(沮丧) – 2014-12-06 09:05:01

+1

@ vks,你非常欢迎。 :) – merlin2011 2014-12-06 09:06:24