Jump to content

Help with some code in Python 2

javier
Go to solution Solved by GhostHitWall,

Not sure if I understand you correctly but I think this is what you are asking.

def catego(lista):
    l1=[]
    for i in lista:
        if i>=0 and i<=12:
            l1.append("Niño")
        elif i>12 and i<=17:
            l1.append("Adolescente")
        elif i>18 and i<=30:
            l1.append("Joven")
        elif i>30 and i<=45:
            l1.append("Maduro")
        elif i>45 and i<=65:
            l1.append("Adulto")
        else:
            l1.append("Adulto Mayor")
    return l1
Edades=[9,45,67,38,23,15,47]
print(catego(Edades))

The above code gives me output like this, 

['Niño', 'Maduro', 'Adulto Mayor', 'Maduro', 'Joven', 'Adolescente', 'Adulto']

 

So i started to define some functions in python 2, but i have a problem with one function this specified function:

def catego(lista):
    i=0
    while i<len(lista):
        if lista<=12 and lista>=0:
            clase="Niño"
        elif lista>12 and lista<=17:
            clase="Adolescente"
        elif lista>18 and lista<=30:
            clase="Joven"
        elif lista>30 and lista<=45:
            clase="Maduro"
        elif lista>45 and lista<=65:
            clase="Adulto"
        else:
            clase="Adulto Mayor"
        i=i+1
    return [clase]

after that i define a list:

Edades=[9,45,67,38,23,15,47]

and i want my function to identify each value from the list and that it throws me another list but now with each respective clase from the function.

the problem is that when i run the function, only throws the clase from the last value of the list. 

Link to comment
Share on other sites

Link to post
Share on other sites

Not sure if I understand you correctly but I think this is what you are asking.

def catego(lista):
    l1=[]
    for i in lista:
        if i>=0 and i<=12:
            l1.append("Niño")
        elif i>12 and i<=17:
            l1.append("Adolescente")
        elif i>18 and i<=30:
            l1.append("Joven")
        elif i>30 and i<=45:
            l1.append("Maduro")
        elif i>45 and i<=65:
            l1.append("Adulto")
        else:
            l1.append("Adulto Mayor")
    return l1
Edades=[9,45,67,38,23,15,47]
print(catego(Edades))

The above code gives me output like this, 

['Niño', 'Maduro', 'Adulto Mayor', 'Maduro', 'Joven', 'Adolescente', 'Adulto']

 

Link to comment
Share on other sites

Link to post
Share on other sites

3 minutes ago, GhostHitWall said:

Not sure if I understand you correctly but I think this is what you are asking.


def catego(lista):
    l1=[]
    for i in lista:
        if i>=0 and i<=12:
            l1.append("Niño")
        elif i>12 and i<=17:
            l1.append("Adolescente")
        elif i>18 and i<=30:
            l1.append("Joven")
        elif i>30 and i<=45:
            l1.append("Maduro")
        elif i>45 and i<=65:
            l1.append("Adulto")
        else:
            l1.append("Adulto Mayor")
    return l1
Edades=[9,45,67,38,23,15,47]
print(catego(Edades))

 

That´s exactly what i wanted to do, thank you a lot

Can i ask you something :what´s the function of append?

Link to comment
Share on other sites

Link to post
Share on other sites

2 minutes ago, javier said:

That´s exactly what i wanted to do, thank you a lot

Can i ask you something :what´s the function of append?

to add something onto the end of a list.

in this case, he's making two lists, and if item 1 in the first list is 9, then he adds "Nino" (sorry can't be fucked to write accents now) to the end of the other list. (called l1)

 

QUOTE/TAG ME WHEN REPLYING

Spend As Much Time Writing Your Question As You Want Me To Spend Responding To It.

If I'm wrong, please point it out. I'm always learning & I won't bite.

 

Desktop:

Delidded Core i7 4770K - GTX 1070 ROG Strix - 16GB DDR3 - Lots of RGB lights I never change

Laptop:

HP Spectre X360 - i7 8560U - MX150 - 2TB SSD - 16GB DDR4

Link to comment
Share on other sites

Link to post
Share on other sites

1 minute ago, RadiatingLight said:

to add something onto the end of a list.

in this case, he's making two lists, and if item 1 in the first list is 9, then he adds "Nino" (sorry can't be fucked to write accents now) to the end of the other list. (called l1)

 

oh so that´s the way the function adds the clases to the list l1, so that´s the reason you put l1=[ ] is to define l1 as a list.

thank you man this explains a lot of things for me.

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×