Geoffrey McCosker
Geoffrey McCosker

Reputation: 1

If statment has invalid syntax

Im am currently trying to make a game within pygame and am in the process of adding some music, the current code I have is this

    if self.room.running = True:
        self.music_sound = self.load_sound('Song.mp3')
        self.music_sound.play()
    else:
        self.music_sound.stop()

The idea is that when executed, the song would play until the room was no longer running and the player had moved onto the next level.

however, when I try to run the code, it comes up with this error

  if self.room.running = True:     
                       ^
  SyntaxError: invalid syntax

Any help would be appreciated :)

Upvotes: 0

Views: 53

Answers (3)

Fajar Ulin Nuha
Fajar Ulin Nuha

Reputation: 399

Another alternative:

if self.room.running:
    self.music_sound = self.load_sound('Song.mp3')
    self.music_sound.play()
else:
    self.music_sound.stop()

Upvotes: 0

user13884248
user13884248

Reputation:

Comparison operators are used to compare two values.

The if statement may be combined with:

== equality,
>= greater than,
<= smaller than
!= not equal

The "=" sign used to assign values to variables.

Upvotes: 0

Capt.Pyrite
Capt.Pyrite

Reputation: 911

Try doing:

if self.room.running == True:
    self.music_sound = self.load_sound('Song.mp3')
    self.music_sound.play()
else:
    self.music_sound.stop()

Upvotes: 4

Related Questions