K
K
Konstantin2020-09-04 17:08:42
Python
Konstantin, 2020-09-04 17:08:42

Doesn't change the value of a variable in python?

I am not changing the value of a variable in python.
An excerpt from my code:

if musicState == "Off":
  musicState = "On"
  info.music = True
elif musicState == "On":
  musicState = "Off"
  info.music = False

The music value in the other file did not change to False.
Can you help?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
F
FrCl, 2020-09-04
@FrCl

1. Apparently, this is a piece of some function, and info is an instance of some class. We need to deal with this instance. Is it the same in all files or is it recreated in different files?
2. The values ​​"On" and "Off" are capitalized here, perhaps somewhere else with a small or even a typo. It would be more logical to use the boolean value True or False, then the whole code would look much simpler:

musicState = not musicState
info.music = musicState

If it is essential to use exactly the On and Off lines, add an additional check to your code:
else:
    assert False, f"musicState is {musicState}"

3. Perhaps musicState is a global variable, and you forgot to write at the beginning of the function
global musicState
4. Do you even need this musicState variable? Mb, is it worth doing just info.music?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question