Pycharm complaining about the local variable

Asked

Viewed 149 times

-3

Next, I have a code made in Python 3.7.2 very simple and basic (I’m still learning and wanted to train a little doing a gym training routine). The program works normally, but Pycharm is complaining (marking the yellow word) of the local variable "color". inserir a descrição da imagem aqui

If I assign any value (from string to number) to that variable before the command for in, Pycharm stops complaining, but I wanted to know why. Someone can help me? (This is my first question on stackoverflow)

Here’s the code for anyone who wants to copy and verify it:

dias = 'Segunda, Terça, Quarta, Quinta, Sexta, Sábado, Domingo'
dias = dias.split(', ')

treino = 'TRÍCEPS/PEITO, PERNAS/ABS, BÍCEPS/COSTAS'
treino = treino.split(', ')

cont = 0   # CONTADOR MÚSCULO

cores = {'tri': '\033[1;34m', 'bic': '\033[1;36m', 'per': '\033[1;31m'}  # 
PALETA DE CORES

for c in range(0, 7):

    print('\033[1;30m+---+---+---+---+---+---+')

    # DEFINE A COR DA STRING DO MÚSCULO TREINADO
    if cont == 0:
        cor = cores['tri']
    elif cont == 1:
        cor = cores['per']
    elif cont == 2:
        cor = cores['bic']

    # QUARTA-FEIRA, DIA DE DESCANSO
    if c == 2:           
        print(f'|\033[1;33m{dias[c<7}\033[1;30m|\033[1;35m{"descanso":^15}
\033[1;30m|')
        cont = 1

    # DIAS NORMAIS, NOS QUAIS HÁ TREINO
    else:
        print(f'|\033[1;33m{dias[c]:<7}\033[1;30m|{cor} 
{treino[cont]:^15}\033[1;30m|')

    cont += 1

    # RESETA O CONTADOR DOS MÚSCULOS
    if cont == 3:
        cont = 0
print('\033[1;30m+---+---+---+---+---+---+')
  • 2

    Always type your code instead of just pasting images showing it. This enables others to reproduce your code and verify it.

  • 2

    You own a if defining the value of cor. If cont is different from 0, 1 and 2, which is the value of cor?

  • @Andersoncarloswoss The cont will never be different from 0, 1 and 2, because when it becomes worth 3, the program assigns the value 0 to it.

  • 1

    @Cadu Sorry, thanks for the recommendation! I already added the code.

  • @Eduardocolho then you don’t need elif cont == 2, if it is not 0 or 1, it will necessarily be 2; you can use the else.

  • @Andersoncarloswoss Thank you very much! It worked now, I had thought about it at the time of creating the program, but I didn’t think this would be a problem for Pycharm.

Show 1 more comment

1 answer

2


Just you position the mouse about the notification that the message will appear that the IDE has missed:

inserir a descrição da imagem aqui

In free translation, the name cor may not be defined. This is a warning, not a error. The warning is more like "Watch out! Something can be wrong".

That’s because you did:

if cont == 0:
    cor = cores['tri']
elif cont == 1:
    cor = cores['per']
elif cont == 2:
    cor = cores['bic']

What about the other conditions? When cont is 3, the variable cor will not be defined. Pycharm is warning you this precisely because it may be a situation you forgot to deal with, which may cause some more serious error in the application.

Did you comment that cont it will never be different from 0, 1 or 2, so why check the 3 values? If it is not 0 or 1, it will certainly be 2, then just do:

if cont == 0:
    cor = cores['tri']
elif cont == 1:
    cor = cores['per']
else:
    cor = cores['bic']

Simpler than that, you can define a dictionary to do this mapping:

cor = {0: cores['tri'], 1: cores['per'], 2: cores['bic']}.get(cont, None)

Basically the dictionary is created:

{
    0: cores['tri'],
    1: cores['per'],
    2: cores['bic']
}

And accessed the position cont. If the position does not exist, the value will be returned None.

Browser other questions tagged

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