How to select and print a value within a list created by a "def"

Asked

Viewed 449 times

1

The title is already self explanatory, as I do to select and choose given str or int float/ inside a list that was created by my record function

def registro(cadastroaluno):
    matricula = input("Número de Matrícula: ")
    telefone = input("Número de Telefone: ")
    endereco = input("Endereço: ")
    respReg = matricula,telefone,endereco
    return respReg

aluni = str
print(registro(aluni))

For example:

How do I select the value '32' located on the line [2] and print her right after?

inserir a descrição da imagem aqui

1 answer

1


What function returns is not a list, it is a tuple (think of it as an immutable list).

Your variable aluni, may cease to exist, on that piece of code is serving no purpose.

As for your doubt, why don’t you do it like this:

def registro():
    matricula = input("Número de Matrícula: ")
    telefone = input("Número de Telefone: ")
    endereco = input("Endereço: ")
    return (matricula,telefone,endereco)

dados = registro() # dentro de dados tens todos os dados que foram introduzidos
print(dados[2])

Or you can even unpack the tuple that returns to stay with the variables separately:

...
matricula, tele, endereco = registro()
print(endereco)

If you are going to use only Indice 2 and delete the others you can do it soon:

def registro():
    matricula = input("Número de Matrícula: ")
    telefone = input("Número de Telefone: ")
    endereco = input("Endereço: ")
    return (matricula,telefone,endereco)

endereco = registro()[2]
print(endereco)

Remember that inputs always return strings, to convert them you can:

...
int(input("Endereço: "))
...
  • I couldn’t assign data to the record (). ?

  • @Kennethanderson how so assign the data? I will put in full

  • I said it wrong, I’m sorry, I’m new to Python. I was saying that I was not managing to make "record()" could not be assigned to "data". I got it now thanks to you, thanks! P.S: Sorry for the explanation/thanks comment.

  • No problem nenum @Kennethanderson, I’m glad I helped

Browser other questions tagged

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