Python - Find a tuple odd

Asked

Viewed 977 times

0

I’m new in this area and recently, for a college job I couldn’t turn this line of code into tuple. function code:

def encontra_impares(lista):

    lis = []

    if len(lista) > 0:

        numero = lista.pop(0)

        if numero % 2 != 0:

            lis.append(numero)

        lis = lis + encontra_impares(lista)

    return lis

function call:

print(encontra_impares([1, 2, 3,5,5,9,7,32,1,6]))

2 answers

1


You can adjust your function to something like:

def encontra_impares( tupla ):

    lista = []

    for n in tupla:
        if n % 2 != 0:
            lista.append(n)

    return lista


tpl = (1, 2, 3,5,5,9,7,32,1,6)
lst = encontra_impares( tpl )

print(tpl)
print(lst)

Or Simply:

def encontra_impares( tupla ):
    return [ n for n in tupla if n % 2 != 0 ]

tpl = (1, 2, 3,5,5,9,7,32,1,6)
lst = encontra_impares( tpl )

print(tpl)
print(lst)

Exit:

(1, 2, 3, 5, 5, 9, 7, 32, 1, 6)
[1, 3, 5, 5, 9, 7, 1]

0

Guy got a little confused about what exactly you want but according to your goal would do this way :

 lis=[]
 lista=[0,1,2,3,4,5,6,8,9]
      for i in range(len(lista)):
         if lista[i]%2 == 0:
         lis.append(lista[i])
  • Write a python function that takes as parameter a tuple of numbers integers. The function should return a list with the odd values of the tuple. , that’s the question

Browser other questions tagged

You are not signed in. Login or sign up in order to post.