This question has become a little difficult to understand, but I’ll try to put it in context. I hope it helps. Regarding the queue, I will use a literal, with people. I will take as something basic for being a question also of algorithm.
More information here on devmedia.
Let’s assume there are already two people
pessoas = ['Keanan', 'Eduard']
Then one more has just arrived. It has come last, so go to the end
pessoas.append('Ayla')
[Keanan, 'Eduard', 'Ayla'']
Continuing... the queue finally walked, then the first one was answered. Soon he leaves the queue
pessoas.pop(0)
Staying: ['Eduard', 'Ayla']
Here things get really interesting. We have several ways to get people out of line. We can call her by name, as in people.remove('Ayla') or by number, as in people.pop(1). It is also possible to use del people1, no parentheses in that case.
At the end of everything, the most important thing is to remember that the counting of the lists starts at zero. Soon at ['Maisie', 'Smith', 'Hannah'] or I am the 1.
For the first element we use [0], and the last [-1].
Colinha:
idades = [10, 33, 5]
idades.append(3) # adiciona ao final
idades.pop(2) # remove o 5... o número dois é o indice
idades.remove(10) # remove o 10... o número dez é o valor
idades[0] # mostra o primeiro
idades[-1] # mostra o último
print(idades) # mostra todos
Here’s an example of code that can solve the problem, I didn’t put everything to let you think a little. On the Insert, it puts the person in a certain position in the queue, with the 0 being the beginning.
sair = False
fila = []
while not sair:
entrada = int(input('''
Selecione uma opção:
- [1] Adicionar
- [2] Adicionar no Inicio
- [3] Remover
- [4] Ver Fila
- [5] Sair \n
'''))
if entrada == 1:
valor = input('O que deseja adicionar? ')
fila.append(valor)
if entrada == 2:
valor = input('O que deseja adicionar ao inicio? ')
fila.insert(0, valor)
if entrada == 5:
sair = True
Hi, then, the idea would be to create a menu, and in it would have the options to insert, remove, add in front of the queue, view all the content of the queue and repeat this "menu" until selecting the option to finish the program. Could you please help me how to do it this way?
– Henrique
I’ll edit the post
– Smith