Search for an element in the list

0
  

Make a program that manages the vestibular result. For your happiness, there is only one course and the course has 10 places. The program must maintain the list of 10 classified. The program also maintains, in another list (20 positions), the waiting list, which contains the   candidates, but exceeding the number of places (if any   give up, it will be called one on the waiting list). The waiting list can have up to 20 candidates.

     

The program should request the applicant's number and say:

     

(a) if it has been sorted, or

     

(b) If it is on the waiting list,   in this case, indicate where in the queue it is, or

     

(c) If   it was not approved.

Example. Are the classified: 20 32 01 14 99 34 21 02 15 07

Be the waiting list (value -1 means there is no candidate): 08 04 10 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1

Examples of queries: Entrance exit 01 "classified" 04 "number 2 on the waiting list 97 "not approved"

What I was able to do:

numeroCandidato = int(input("Entre com o numero do candidato: "))
classificados = [20,32,1,14,99,34,21,2,15,7]
listaDeEspera = [8, 4, 10]
if numeroCandidato not in classificados:
print("nao aprovado")
    
asked by anonymous 12.05.2017 / 15:19

1 answer

1

You've come quite close to the final solution. To check if the candidate has been approved, simply check if your number is on the classified list:

if numeroCandidato in classificados:
    print("Aprovado")

To check if the candidate is on the waiting list, simply check if your number is on the waiting list:

elif numeroCandidato in listaDeEspera:
    print("Lista de espera")

However, you are also asked to indicate which position on the waiting list the candidate is in. For this, we can use the method index of object listaDeEspera to obtain the position of the candidate. Remember that as the indexes of a list in Python start at 0, we will need to increment the value by one, so that if the candidate is in position 0 of the list, it appears position 1:

elif numeroCandidato in listaDeEspera:
    posicao = listaDeEspera.index(numeroCandidato) + 1
    print("Número %d na lista de espera." % posicao)

Finally, if none of the above conditions are satisfied, it should appear that the candidate has not been approved:

else:
    print("Não aprovado")

The final code would then be:

numeroCandidato = int(input("Entre com o numero do candidato: "))

classificados = [20,32,1,14,99,34,21,2,15,7]
listaDeEspera = [8, 4, 10, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1]

if numeroCandidato in classificados:
    print("Aprovado!")
elif numeroCandidato in listaDeEspera:
    posicao = listaDeEspera.index(numeroCandidato) + 1
    print("Número %d na lista de espera." % posicao)
else:
    print("Não aprovado.")
    
12.05.2017 / 16:04