如何检查数字中连续的数字是偶数还是奇数?

How to check if consecutive digits in a number are even or odd?

我正在做一些练习和学习Python。我需要能够检查输入数字的连续数字是偶数还是奇数。因此,如果第一个数字是奇数,下一个应该是偶数,依此类推才能符合条款。我有以下代码:

def par_nepar(n):
    cifre = []

    while n != 0:
        cifre.append(n % 10)
        n //= 10

    cifre = cifre[::-1]
    ind = cifre[0]
    for broj in cifre:
        if cifre[0] % 2 == 0:
            # for br in range(len(cifre)):
            if cifre[ind + 1] % 2 != 0:
                ind = cifre[ind+1]

n = int(input("Unesite broj n: "))
print(par_nepar(n))

如您所见,我正在努力处理索引循环。我接受了输入数字并将其转换为列表。为 index[0] 创建了一个变量,并不知道如何遍历连续的索引。我知道我可以使用 zip 或 enumerate 但我认为这不是真正的 pythonic 解决方案,可能有一种更简单的方法来循环连续的列表编号并将它们与 index[-1].

进行比较

输入示例:

>>>print(par_nepar(2749)) # Every consecutive digits shifts odd->even or even->odd
True
>>>print(par_nepar(2744)) # Two last digits are even
False

我认为与其获取整数作为输入,不如获取字符串并将其转换为整数列表

def par_nepar(n):
    s,h=0,0
    for i in range(len(n)-1):
        if n[i]%2==0 and n[i+1]%2!=0:
            s+=1
        elif n[i]%2!=0 and n[i+1]%2==0:
            h+=1
    if s==len(n)//2 or h==len(n)//2:
        print("The number complies to the needed terms")
    else:
        print("The number does not complies to the needed terms")

# list of digits in the provided input
n = list(map(lambda x: int(x),list(input("Unesite broj n: "))))
par_nepar(n)
def par_nepar(n):
    cifre = list(str(n))
    flg = False
    if int(cifre[0]) % 2 == 0:
        flg = True
    for d in cifre[1:]:
        _flg = False
        if int(d) % 2 == 0:
            _flg = True
        if not flg^_flg:
            print("The number does not complies to the needed terms")
            return
        flg = _flg
    print("The number complies to the needed terms")
    return

试试这个:

def par_nepar(n):

    split = [int(i) for i in str(n)]. 

    for i in range(1,len(split)):

        if (split[i] % 2 == 0) ^ (split[i-1] % 2 == 1):
            return False

    return True

工作为:

  1. 将整数转换为列表:1234 -> [1,2,3,4]
  2. 迭代元素(不包括第一个)
  3. 如果两个连续数字是偶数或奇数,则采用 False 的 XOR 条件。

测试:

>>> print(par_nepar(2749))
True

>>> print(par_nepar(2744))
False

我的解决方案非常简单。只需稍微更改您的代码并避免使用索引,只需循环遍历 cifre 中的所有数字并处理布尔标志:

def par_nepar(n):
    cifre = []

    while n != 0:
        cifre.append(n % 10)
        n //= 10

    even = True
    odd = True
    output = "The number complies to the needed terms"

    for broj in cifre:
        if broj % 2 == 0 and odd:
            even = True
            odd = False
        elif broj % 2 != 0 and even:
            odd = True
            even = False
        else:
            return "The number doesn't comply to the needed terms."
    return output
n = int(input("Unesite broj n: "))
print(par_nepar(n))

输出:

Unesite broj n: 33890
The number doesn't comply to the needed terms.

Unesite broj n: 4963850
The number complies to the needed terms

也许你也会喜欢这一款。

在一个循环中,我们查找数字 mod 2 是否与(第一个数字 + 索引)mod 2.

相同

这适用于您交替用例。

par_nepar = lambda x: all([(int(k) % 2) == ((i + int(str(x)[0])) % 2) for (i, k) in enumerate(str(x))])

print(par_nepar(2749))
print(par_nepar(2744))

# Output
True
False

或者如果你想要你说的字符串,稍微大一点

par_nepar = lambda x: 'The number complies to the needed terms. ' if all(
    [(int(k) % 2) == ((i + int(str(x)[0])) % 2) for (i, k) in
     enumerate(str(x))]) else "The number doesn't comply to the needed terms."

#Output
The number complies to the needed terms.
The number doesn't comply to the needed terms.