I'm a new programmer so this is probably not efficient but I'm finding this issue curious to me. Look at the function countDigitsCheck below and notice the if statement is checking if the variable, count_check_one and variable, count_check_two == 1. If they are both == 1 then it multiplies a * b. You can see in the output that count_check_one is == 3 (not 1) and count_check_two == 1. Since one of the integers is == 3 the two integers should NOT multiply. However, you'll notice in the output the integers are multiplying to 900. I understand that if I had used "or" instead of "and" in the if statement I would expect this behavior, but not when I use "and". Please help me to understand why they are still multiplying. Thanks.
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
Aucun commentaire:
Enregistrer un commentaire