Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

No idea on how to create a function that determines if a number has an even digit within

I am learning to create functions. The whole program basically prints only the even digits of a number, but I need to create a function that, in case the number has no even digits, will print that it doesn’t have any, but I have no idea on how to make the condition.
Thanks in advance.

So far this is the code without the function I need:

def imprimirPares (num):
    while num != 0:
        resultado = 0
        i = 1
        digito = num % 10
        if digito % 2 == 0:
            resultado += digito * i
            i *= 10
        num // 10
    return resultado

def ingresarEyS (num):
    print ("Nuevo numero con los valores pares de la cifra: ")
    num = ()
    print ("El nuevo numero es ",imprimirPares(num),".")

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Using your already existing function this is fairly easy. One error you have made is that you do not assign the result of num // 10 to num though.

def has_even_digit(num):
    while num != 0:
        result = 0
        i = 1
        digit = num % 10
        if digit % 2 == 0:
            # even digit found
            return True
        else:
            result += digit * i
            i *= 10
            num = num // 10
    return False


print(has_even_digit(45))
print(has_even_digit(13))
print(has_even_digit(1579315))
print(has_even_digit(780278452))

Expected output:

True
False
False
True
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading