我得到输出True,尽管它应该是False。

huangapple go评论56阅读模式
英文:

I get the output True even though it should be False

问题

Here's the translated code portion:

def par_analy(num):
    x = str(num)
    sum = int(x[0])
    if len(x) != 1:
        for i in range(1, len(x)):
            sum += int(x[i])
        par = num % 2
        sumi = sum % 2
        if par and sumi == 0:
            return True
        elif par and sumi != 0:
            return True
        else:
            return False
    else:
        return True
print(par_analy(13))

The code determines whether the sum of the digits of a given number has the same parity (odd or even) as the number itself and returns True or False accordingly.

英文:

Create a function that takes a number as input and returns True if the sum of its digits has the same parity as the entire number. Otherwise, return False.

e.g.

parity_function(243) --> True

243 is odd and so is 9 (2 + 4 + 3)

def par_analy(num):
    x = str(num)
    sum = int(x[0])
    if len(x)!=1:
        for i in range(1,len(x)):
            sum += int(x[i])
        par = num % 2
        sumi = sum % 2
        if par and sumi == 0:
            return True
        elif par and sumi !=0:
            return True
        else:
            return False
    else:
        return True
print(par_analy(13))

The output is True even though both par and sumi don't equal 0 or equal 0

我得到输出True,尽管它应该是False。

答案1

得分: 2

I think it should be:

def par_analy(num):
    x = str(num)
    sum = int(x[0])
    if len(x) != 1:
        for i in range(1, len(x)):
            sum += int(x[i])
        par = num % 2
        sumi = sum % 2
        if par == 0 and sumi == 0:
            return True
        elif par != 0 and sumi != 0:
            return True
        else:
            return False
    else:
        return True

print(par_analy(13))

When you write something like if par and sumi == 0, you're saying that par has a truthy value, and sumi is zero.

英文:

I think it should be:

def par_analy(num):
    x = str(num)
    sum = int(x[0])
    if len(x)!=1:
        for i in range(1,len(x)):
            sum += int(x[i])
        par = num % 2
        sumi = sum % 2
        if par == 0 and sumi == 0:
            return True
        elif par != 0 and sumi !=0:
            return True
        else:
            return False
    else:
        return True
print(par_analy(13))

When you write something like if par and sumi == 0 you're saying that par has a truthy value, and sumi is zero.

答案2

得分: 0

这是一个Python函数,它接受一个数字作为输入,如果其各位数字之和与整个数字的奇偶性相同则返回True,否则返回False:

def parity_function(num):
    digit_sum = sum(int(digit) for digit in str(num))
    return num % 2 == digit_sum % 2

它的工作原理如下:

  • str(num) 调用将输入的数字转换为字符串,以便我们可以使用循环迭代其各位数字。
  • 循环内的 int(digit) 转换将每个数字从字符串转换为整数。
  • sum() 函数将所有数字相加。
  • 表达式 num % 2 == digit_sum % 2 比较输入数字 num 的奇偶性与数字和 digit_sum 的奇偶性,如果它们相同则返回True,否则返回False。

请注意,该函数假定输入是正整数。如果需要处理负整数或其他类型的输入,可能需要添加额外的错误检查代码。

英文:

Here's a Python function that takes a number as input and returns True if the sum of its digits has the same parity as the entire number, and False otherwise:

def parity_function(num):
    digit_sum = sum(int(digit) for digit in str(num))
    return num % 2 == digit_sum % 2

Here's how it works:

  • The str(num) call converts the input number to a string so we can iterate over its digits using a loop.
  • The int(digit) conversion inside the loop converts each digit from a string to an integer.
  • The sum() function adds up all the digits.
  • The expression num % 2 == digit_sum % 2 compares the parity (evenness or oddness) of the input number num to the parity of the digit sum digit_sum, and returns True if they are the same and False if they are different.

Note that this function assumes that the input is a positive integer. If you need to handle negative integers or other types of input, you may need to add additional error checking code.

答案3

得分: 0

Shouldn't you be returning False in this case?
不应该在这种情况下返回False吗?

英文:
def par_analy(num):
    x = str(num)
    total_sum = 0
    for i in range(len(x)):
        total_sum += int(x[i])
    parity = num % 2
    sum_parity = total_sum % 2
    return parity == sum_parity

elif par and sumi !=0: return True<br>
Shouldn't you be returning False in this case?

答案4

得分: 0

这个函数首先将输入的数字 n 转换成字符串,以提取其各个数字。然后,它使用一个生成器表达式在 sum() 函数内计算这些数字的总和。最后,它通过比较它们除以 2 的余数来检查原始数字和数字总和是否具有相同的奇偶性(偶数或奇数)。如果余数相同,函数返回True,否则返回False。

英文:
def parity_function(n):
# Convert the number to a string to extract its digits
digit_str = str(n)

# Calculate the sum of the digits
digit_sum = sum(int(d) for d in digit_str)

# Check if both the number and the sum of its digits have the same 
parity
return (n % 2 == 0) == (digit_sum % 2 == 0)

# Test the function
print(parity_function(243))  # True

This function first converts the input number n into a string to extract its digits. Then, it calculates the sum of these digits using a generator expression inside the sum() function. Finally, it checks if both the original number and the digit sum have the same parity (even or odd) by comparing their remainders when divided by 2. If the remainders are the same, the function returns True, otherwise False.

huangapple
  • 本文由 发表于 2023年4月20日 04:13:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/76058464.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定