-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".
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+---+---+---+---+---+---+')
Always type your code instead of just pasting images showing it. This enables others to reproduce your code and verify it.
– Cadu
You own a
if
defining the value ofcor
. Ifcont
is different from 0, 1 and 2, which is the value ofcor
?– Woss
@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.
– Eduardo Coêlho
@Cadu Sorry, thanks for the recommendation! I already added the code.
– Eduardo Coêlho
@Eduardocolho then you don’t need
elif cont == 2
, if it is not 0 or 1, it will necessarily be 2; you can use theelse
.– Woss
@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.
– Eduardo Coêlho