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

Python 如何检查数字中的连续数字是偶数还是奇数?,python,python-3.x,list,function,nested-lists,Python,Python 3.x,List,Function,Nested Lists,我正在做一些练习,学习Python。我需要能够检查输入的数字,无论其连续数字是偶数还是奇数。因此,如果第一个数字是奇数,那么下一个数字应该是偶数,依此类推,以使条款符合要求。我有以下代码: def par_nepar(n): cifre = [] while n != 0: cifre.append(n % 10) n //= 10 cifre = cifre[::-1] ind = cifre[0] for broj

我正在做一些练习,学习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))
正如您所看到的,我正在与索引循环作斗争。我把输入的数字转换成一个列表。为索引[0]创建了一个变量,但实际上不知道如何循环遍历连续的索引。我知道我可能可以使用zip或enumerate,但我认为这不是一个真正的Python解决方案,可能有一种更简单的方法来循环连续的列表编号,并将它们与索引[-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):

    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
工作内容如下:

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

    >>> 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.
    

    你能举一个输入和期望输出的简单例子吗?这可能会有所帮助,谢谢。接受字符串作为输入,然后将其转换为数字列表背后的逻辑/推理是什么?因为程序希望迭代给定数字的数字。y Q保持不变。我认为除了数据类型转换之外,还有其他方法。@mnm在这个问题中,他得到一个整数输入,并将其传递到函数中,然后使用while循环将整数转换为数字列表。我没有用很长的时间来做这件事,而是用一个字符串来输入,而不是转换成整数列表,这在单行中完成了上面的过程,这让你很难过,但我的Q仍然保持不变。此外,没必要向我解释OP想要什么或你提出了什么建议。我要重申,我的观点是可以避免数据类型转换吗?此外,这里不考虑单线解决方案或多线解决方案。
    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.