0
What would be the problem with these codes?
Someone could guide me which is the best practice to make a system of registration, editing, exclusion and consultation?
First code:
class Cadastro:
def __init__(self):
self.nome = input('Digite nome do aluno: ')
self.senha = input('Digite a senha: ')
def aluno(self, nome, senha):
self = Cadastro()
self.nome = ' '
self.senha = ' '
return self
Second code:
class Cadastro:
def __init__(self):
self.nome = input('Digite nome do aluno: ')
self.senha = input('Digite a senha: ')
def aluno(self, nome, senha):
self = Cadastro()
self.nome = input('Digite nome do aluno: ')
self.senha = input('Digite a senha: ')
return self
Third code:
**from lista import * list = [] while True:
opc = int(input('Cadastrar, digite 1\nConsultar, digite 2 \nOpção: '))
print()
if opc == 1:
banco = (NovaConta())
lista.append(banco)
print('Cadastrado com sucesso! \nID: %i \nCliente: %s \nConta: %i '
%(banco.ID, banco.cliente, banco.conta))
print('-'*25)
elif opc == 2:
consulta = int(input('Digite o ID: '))
if consulta not in lista:
print('Não contém')
else:
print(consulta[lista])**
And saved this file with the following list name.py:
class usuario:
ID = 0
cliente = ' '
conta = 0
def NovaConta():
nc = usuario()
nc.ID = int(input('Digite um ID): '))
nc.cliente = input('Digite Nome: ')
nc.conta = int(input('Digite número da conta: '))
print()
return nc
Thanks! I’ll try to understand.
– Nunes
Pedro. Good evening! So, I am evaluating your code first and I was doubtful. def listar_alunos(self): for i, aluno in enumerate(self.alunos): print(i, aluno.nome) esse for concatenating i and that variable student what role they play. That was one of the doubts. Gratefully!
– Nunes
The function list students shows on the screen the student’s number and then his name. The loop
for
iterate over the listalunos
given, and for each element in the list gives the namealuno
and executes what is inside your block. Theenumerate
causes instead of only returning each element of the list, thefor
also return the element number. I mean, when the is passed by the first student the value ofi
is 0, the second is 1, and so on. So when we doprint(i, aluno.nome)
, we are printing the number of each element of the listalunos
and the corresponding student’s name.– Pedro von Hertwig Batista
My doubt, was no for enumerate. Valeu Pedro!
– Nunes
Good morning Pedro! What does this part of the code do? f name == 'main': interface = Interface() interface.loop() Would you be so kind as to use it in another simple example? I’ve never seen it and I’m seeing it now constantly and I can’t understand what it does. Grateful.
– Nunes
Oops! Basically, it serves to run the code only if the main file is called. This serves to avoid multiprocessing confusions and to have different behavior when importing a file as a module or running it directly. Here are some questions with more comprehensive answers: 1 2
– Pedro von Hertwig Batista
From what I understand, main is a function that loads a file without having to invoke the function. Correct, more or less that?
– Nunes