Python返回正确或错误

Mar*_*art 1 python python-3.x

我不相信我设置正确...因为无论我为foo()填写什么数字,它似乎总是返回"True".我究竟做错了什么??

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

mer*_*011 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))
Run Code Online (Sandbox Code Playgroud)

Python 2的输出:

python TrueMe.py 
0
False
2
False
Run Code Online (Sandbox Code Playgroud)

Python 3的输出:

python3 TrueMe.py 
0.6666666666666666
True
2.0
False
Run Code Online (Sandbox Code Playgroud)

不用说,输入2,2,2确实会导致产生返回值False.

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