带有关键字的条件 if 语句"and"给我意外的结果



我是一名新程序员,所以这可能效率不高,但我发现这个问题很奇怪。看看下面的函数countDigitsCheck,注意到if语句正在检查变量count_check_one和变量count_ccheck_two==1。如果它们都是==1,那么它乘以a*b。你可以在输出中看到count_check_one是==3(而不是1(,count_check_two==1。因为其中一个整数是==3,所以两个整数不应该相乘。但是,您会注意到输出中的整数正在乘以900。我知道,如果我在if语句中使用了"或"而不是"one_answers",我会期望这种行为,但当我使用"one_answers"时就不会了。请帮我理解为什么它们还在繁殖。谢谢

def karatsubaAlgorithm(x,y):
return(countDigitsCheck(x,y))
def countDigitsCheck(one, two):
count_check_one = countDigits(one)
count_check_two = countDigits(two)
a = one
b = two
print(count_check_one)
print(count_check_two)
if count_check_one and count_check_two == 1:
answer = a * b
return(answer)
def countDigits(number):
Number = number
Count = 0
while(Number > 0):
Number = Number // 10
Count = Count + 1
return(Count)
check_function = karatsubaAlgorithm(100,9)
print(check_function)

OUTPUT:
3
1
900

您的if语句本质上是if (count_check_one) and (count_check_two == 1):这意味着,只要count_check_one的值不为0,第一个条件就会为true,因为在Python中,除了0之外,每个整数的计算结果都为true。如果要检查count_check_one和count_check_two是否都等于1,则必须执行if count_check_one == 1 and count_check_two == 1:。这将检查两个变量是否都为1。