sublists in python

0

Good morning, I need help with this statement:

"Modify the previous program so that it takes into account the gender of the recipient, for that, they will have to receive a list of lists, with name and gender."

The previous program is as follows:

cont=0
lista=[]

nombre=""
while cont<=5:
    nombre=input("Escribe un nombre: ")
    lista.append(nombre)
    cont=cont+1

p=int(input("Escribe un posición: "))
n=int(input("Escribe una cantidad: "))

for nom in lista[p:n+1]:
    print ("Querido",nom ,"me alegro de verte")

What makes this program is that it allows you to create a list with a series of names that the user enters, then the user enters the starting position of the list and the number of names that he wants to see at the end. Now I have to modify it so that I can do what it asks in the above statement.

Thanks

    
asked by Alex 23.11.2017 в 17:08
source

1 answer

1

Lists is a type of data that can store any other type of data, such as another list, in your case you only have to add append([nombre, genero]) .

Python can unpack lists very easily, in your case you could use for nom, gen in lista[p:n+1]:

Code:

lista=[]

cont = 0
while cont<=5:
    nombre=input("Escribe un nombre: ")
    genero=input("Indique su genero: ")
    lista.append([nombre, genero])
    cont += 1

p=int(input("Escribe un posición: "))
n=int(input("Escribe una cantidad: "))

for nom, gen in lista[p:n+1]:
    print ("Querido",nom ,"me alegro de verte, tu genero es ", gen)

Note: In python it is not necessary to declare the variables, so the creation of nombre="" before the while is unnecessary.

    
answered by 23.11.2017 / 17:16
source