Error in display of dictionary information

Asked

Viewed 43 times

0

I am early in python and I am having a difficulty in the following code:

    class Gerador:
    def __init__(self, nome, potencia, capacidade_energia, capacidade_combustivel):
        self.__nome = nome
        self.__status = None
        self.__potencia = potencia
        self.__capacidade_energia = capacidade_energia
        self.__tanque_combustivel = 0                           
        self.__capacidade_combustivel = capacidade_combustivel 

    def abastecer(self, litros): # abastecer geradores
        if self.__tanque_combustivel + litros <= self.__capacidade_combustivel:
            self.__tanque_combustivel += litros
            print("\nTanque abastecido")
        else:
            print("Erro. Ultrapassou a capacidade")

    def acionamento(self, estado, nome_gerador): # acionar geradores
        self.__nome == nome_gerador
        if nome_gerador == 'G1':
            print('\nATENÇÃO\nG1 não é gerenciado manualmente!')
        elif  estado == '1':
            estado = 'ligado'   
            print(nome_gerador, 'foi ligado!')
        elif estado == '2':
            estado = 'desligado'
            print(nome_gerador, 'foi desligado')
        else:
            print('opção invalida')

    def get(self, nome):
        return self.__nome

    def status_gerador(self, geradores, estado):
        for n in geradores:  
          print (n, geradores[n],[estado])

    def get_status(self):   
        return self.__status

#programa principal
g1 = Gerador("G1", 2000, 1000, 300)
g2 = Gerador("G2", 1300, 530, 435)
g3 = Gerador("G3", 1200, 520, 420)
g4 = Gerador("G4", 1100, 510, 410)  

# abastecimento dos geradores
nome_gerador = input('\nInforme o Nome do Gerador: ')
nome_gerador = nome_gerador.upper()
litros = int(input ('\nQuantidade de Litros: '))
if nome_gerador == 'G1':
    g1.abastecer(litros)# chama o metodo do objeto correspondente
if nome_gerador == 'G2':
    g2.abastecer(litros)
if nome_gerador == 'G3':
    g3.abastecer(litros)
if nome_gerador == 'G4':
    g4.abastecer(litros)

# acionamento dos geradores
nome_gerador = input('\nnome do gerador: ')
nome_gerador = nome_gerador.upper()
print('\ndeseja ligar o gerador ' +nome_gerador+'?\n')
print('1.sim \n2.não\n')
estado = input('Digite: ') 
estado = estado.lower()
if nome_gerador == 'G1':
    g1.acionamento(estado, nome_gerador)
elif nome_gerador == 'G2':
    g2.acionamento(estado, nome_gerador)
elif nome_gerador == 'G3':
    g3.acionamento(estado, nome_gerador)
elif nome_gerador == 'G4':
    g4.acionamento(estado, nome_gerador)
else:
    print('O gerador não existe')

# status dos geradores
print('\nSTATUS DOS GERADORES:')
geradores = {'G1': g1.get_status(), 'G2': g2.get_status(), 'G3': g3.get_status(), 'G4': g4.get_status()}  
g1.status_gerador(geradores, estado) 

when I call the function "status_generator" it brings me the dictionary with results like, "None" and the option type when the function "drive" was called, when in fact what was expected was the name of the generator and its update (on or off), ex: "G2 on". It is important to make it clear that I do not want the complete solution of my problem, just a "north" to follow, thanks in advance for the attention!

  • I recommend studying a little more object orientation, because its class Gerador not making much sense, especially the methods acionamento and status_gerador.

2 answers

0

There are some good modifications that could be made in your code, such as a decrease in the amount of conditions, among others, but this happens, and over time you will work out these details, so focusing on your question I have the following observations:

Soon in the class builder, you establish that self. _status = None, but at no point in your code do you update this value to any other, I think there was some confusion between this class attribute and the variable state, which in this case can be 1 or 2, depending on the user input.

Well, some modifications that could be made would be the following:

def __init__(self, nome, potencia, capacidade_energia, capacidade_combustivel,status=False):
  #.....
  self.__status = status

That is, in the constructor you define an optional argument, which if it is not passed, will already be set with False, which in this situation would represent disconnected.

Also, note that in your method drive, you are setting the state to connected or disconnected, what I believe should be the self_status that should be modified. Example:

def acionamento(self, estado, nome_gerador): # acionar geradores
  #.........
  elif  estado == '1':
      self.__status = True  

Therefore, it will only be necessary to deal with the output, being turned on' if it is True, and turned off if it is False.

By doing so, the state would not even need to be passed as an argument in its method status_generator.

-1

friend, I believe you should define the initial state as different from none or, to facilitate code, use True as bound and False as disconnected, by means of a method that assesses the.

If generator 1 is different from the others, consider making a mother class for generators, a generic generator class and a generator class 1, since it is specific.

Browser other questions tagged

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