Find an item in the list

Asked

Viewed 15,619 times

1

Make a program that manages the result of the entrance exam. For your happiness, there is only one course and the course has 10 vacancies. The program must keep the list of the 10 classifieds. The program also keeps, in another list (of 20 positions), the waiting list, which contains the successful candidates, but in excess of the number of vacancies (if any candidate is classified give up, one of the waiting list will be called). The waiting list may have up to 20 candidates.

The program must request the applicant’s number and say:

(a) whether it has been classified, or

(b) if he is on the wait list, in this case, indicate where in the queue it is, or

(c) if he was not approved.

Example. Be the classifieds: 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

Examples of queries are: Input / Output 01 "classified" 04 "number 2 on waiting list 97 "not approved"

What I could 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")
  • You can [Edit] your question and add the code to at least one of your attempts. This will make it easier for us to guide you, knowing where you are going wrong. And this statement is very confusing to understand. Did you complete it or omit some parts? What information will the entry be? The candidate’s note? What will be the condition to define whether it has been classified?

  • Good morning! So I tried to do the exercise but I did almost nothing... Now on the interpretation: The entry will be the candidate’s number, if the number you entered is in the list of classifieds, a message will appear saying that it has been classified. If you are on the waiting list, a message will appear stating the candidate’s position on the waiting list, and if the number is not on any of the waiting lists, it will appear that it has not been ranked. Theoretically this is how the functionality.

  • If you did almost nothing, you must have done something. Start posting this something, at least to know if you started on the right track.

  • I posted as far as I could, I can’t get out of there

1 answer

2


You’ve come very close to the final solution. To check if the candidate has been approved, just check if your number is on the list of classified:

if numeroCandidato in classificados:
    print("Aprovado")

To check if the candidate is on the waiting list, just check if his number is on the waiting list:

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

However, it is also asked to indicate the position of the waiting list the candidate is in. To this end, we can use the method index of the object listaDeEspera to obtain the candidate’s position. Remember that since the indexes of a Python list start at 0, we will need to increment the value in one, so that if the candidate is at position 0 of the list, 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 is met, it should appear that the applicant has not been approved:

else:
    print("Não aprovado")

The final code then would 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.")
  • So, Anderson, I copied the code you created and made an error when I type in some number that’s on the waiting list. The following is printed: http://imgur.com/a/7O9Y2

  • What will be the problem in this case? And already taking advantage of the question, not to extend too much by simple things. The teacher told us to do this exercise using "For", I tried to do it and I couldn’t. How would I use "For"? Thanks in advance!!

  • The problem was in the version of Python, which does not yet exist the implementation of format. I will change the answer. As for the for, I don’t see how to use it in this solution efficiently.

  • Okay, I’m waiting. I can’t use "For" in this program either, so I got confused.

  • @Pigot believe it will now work. I really don’t see how to use for in this solution.

  • It worked really well. Thank you very much for the attention, thank you very much! I am new here on the site, have any way to thank? Like getting a reputation and stuff? Thanks.

  • On the left side of the answer, there is how you vote positively, indicating that the answer was useful and a little further down an icon v to indicate that the answer solved the problem. You can read more about here.

Show 2 more comments

Browser other questions tagged

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