I am doing some exercises and learning Python. I need to be able to check for an input number whether its consecutive digits are even or odd. So, if the first number is odd, the next should be even and so on in order for the terms to comply. I have the following code:
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))
As you can see, I am struggling with index looping. I took the input number and transformed it into a list. Created a variable for index[0] and don't really know how to loop through consecutive indexes. I know I might be able to use zip or enumerate but I think that is not a truly pythonic solution here and probably there is a simpler way to loop over consecutive list numbers and compare them to index[-1].
Input examples:
>>>print(par_nepar(2749)) # Every consecutive digits shifts odd->even or even->odd
True
>>>print(par_nepar(2744)) # Two last digits are even
False