Creates queue as if it were from a bank (with preferential)

I need to create a program that simulates the registration of people in a hospital queue, obeying the order that it is always a preferred client to then be served an ordinary client.

My doubt is:

    opcao = int(input(''' Emitir senha comum : [1]
    
    
    Emitir senha preferencial : [2]
    Chamar paciente [3]
    
    Pular paciente [4]
    
    Encerrar consulta [5]
    
    Exibir pacientes [6]
    
    
    Exibir tempo médio de espera [7]
    
    Exibir tempo médio de atendimento [8]
    
    xxxxxx [9]
    
    sair [10]
    '''))

This is the program menu, when the user enters option 3 (Call patient), I could not think of a way to make the program read the registered users (they are registered within a dictionary where the key starts from 0 and it follows in ascending order and the items of the keys are the name of the patient, his password and the arrival time) and call in the order of 50%, that is, first a preferential, then it automatically calls a common, only to be able to make this rule of 50% be obeyed with the if, where the receptionist would have to have the notion of who was the last served, if it was a preferential or normal.

Any tips on how to make Python read this rule within two different dictionaries and know which one to call? (there is a dictionary for the common queue and another for the preferred queue)

Author: Woss, 2019-12-13

3 answers

You can try something like:

from random import randint
fila_comum = [{48 : ["fulano", "10:34"]},
    {49 : ["sicrano", "10:35"]},
    {50 : ["beltrano", "10:35"]}
         ]
fila_pref = [{12 : ["fulano", "10:34"]},
    {13 : ["sicrano", "10:35"]},
    {14 : ["beltrano", "10:35"]}
            ]
filas = {0:fila_pref,
           1:fila_comum}

def chamar_paciente(fila):
    paciente = fila.pop(0)
    return paciente

def escolhe_fila():
    escolha = randint(0,1)
    return chamar_paciente(filas[escolha])

With the pop () function, you remove an item from the list and store it in a variable. Thus, you go through and remove each dictionary from the list of items randomly.

For example in calls:

escolhe_fila()
{12: ['fulano', '10:34']}
escolhe_fila()
{13 : ["sicrano", "10:35"]}
escolhe_fila()
{48: ['fulano', '10:34']}
 0
Author: de_python, 2019-12-13 10:33:43

You could just create a list ordem = [] they will already be enumerated from 0 to the last value it has and the order of the list will be in the order they were added, and put dictionaries with the append ordem.append({'preferencial': 1, 'senha': 43}).

Another option is to use from datetime import datetime, to add the time that

from datetime import datetime

ordem.append({'preferencial': 1, 'chegada': datetime.today().now().time()})

And also use a module from itertools import cycle

from itertools import cycle

pref = cycle((0, 1))

So you could reverse between preferred and common, being 0 = False and 1 = True, and whenever you call option 3 run this command switching the preferred and searching the list for a dictionary with the preferred value as 1 or 0

next(pref)
for o in ordem:
    if pref == o['preferencial']:
        # command
        break

And when it finds use the command del to remove from the list and collect the value so that the password does not repeat it

senha_atual = o
del o
break

I gave some suggestions so that you can get an idea of how you can do, but it is part of your learning to accomplish this challenge, any doubt I can try to help

 0
Author: Guilherme França de Oliveira, 2019-12-13 13:27:27
fila_comum = [{48 : ["fulano", "10:34"]},
{49 : ["sicrano", "10:35"]},
{50 : ["beltrano", "10:35"]},
{51 : ["beltrano", "10:35"]}
         ]
fila_pref = [{12 : ["fulano", "10:34"]},
{13 : ["sicrano", "10:35"]},
{14 : ["beltrano", "10:35"]}
            ]
ordem=['fila_pref']

        
def senha(): 
    global ordem
    if len(fila_pref)==0:
        try:
            print(fila_comum.pop(0))
        except:
            print('Não há ninguem na fila!!')
    else:
        if len(fila_comum)==0:
            print('Não há ninguem na fila!!')
        elif ordem[0]=='fila_pref':
            print(fila_pref.pop(0))
            ordem=['fila_comum']

        else:
            print(fila_comum.pop(0))
            ordem=['fila_pref']

Enter a list to switch between queues: the order list changes whenever a password is requested. thus returning one password from each queue at a time.

 0
Author: Juliano Rodrigues, 2021-01-18 12:15:23