I am unable to register the student in the list laluno, when I show the list it is empty

Asked

Viewed 31 times

-1

cad = open('cadastro.txt','w')  
aluno = []  
laluno = []  
print("""           OPÇÔES
      1 - cadastrar novo aluno
      2 - listar alunos cadastrados
      3 - buscar aluno
      4 - remover aluno  """)  
 opt = input('Digite uma opção: ')   

def cadastrar_aluno():  
    aluno = str(input("Digite o nome do aluno:"))  
    aluno=aluno.upper()    

def listar_alunos ():   
    print('Alunos Matriculados', laluno)  

if opt == '1':   
   cadastrar_aluno()  
   laluno.append(aluno)  

elif opt == '2':  
    listar_alunos()   
  • The variable aluno within the function cadastrar_aluno() is different from aluno out. Change this variable inside the function does not change it out. Try to redo using a return aluno in function cadastrar_aluno() and then taking the value of return with aluno = cadastrar_aluno().

1 answer

0


The list is empty because after choosing an option and filling in with the data, the program is terminated. You missed placing your logic inside a while True: to ensure that the program will only be finished when the user requests it. Naturally, there must also be an option to close the programme.

Also, everything is easier if you add the student to the list within the function cadastrar_aluno.

As a courtesy, I modified your program with the above observations, so you understand better. I also removed some unnecessary lines. Look how it turned out:

laluno = []

def cadastrar_aluno():
    aluno = str(input("Digite o nome do aluno: "))
    aluno = aluno.upper()
    laluno.append(aluno)

def listar_alunos ():
    print('Alunos Matriculados:', laluno)

while True:
    print("""           OPÇÕES
        0 - SAIR
        1 - cadastrar novo aluno
        2 - listar alunos cadastrados
        3 - buscar aluno
        4 - remover aluno""")
    opt = input('Digite uma opção: ')

    if opt == '0':
        break
    elif opt == '1':
        cadastrar_aluno()
    elif opt == '2':
        listar_alunos()

Browser other questions tagged

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