Invalid syntax help

I am trying to follow along with a tutorial to write a MUD using python and I am stuck on an invalid syntax that I cannot figure out. Thank you in advance.

What is supposed to happen is when I walk into a room with a creature, I should get a different message depending on if it is dead or alive.

class EnemyTile(MapTile):
    def __init__(self, x, y):
        r = random.random()
        if r < 0.50:
            self.enemy = enemies.GiantSpider()
            self.alive_text = "A giant spider jumps down from its web in front of you!"
            self.dead_text = "A corpse of a dead spider rots on the ground."
        
        elif r < 0.80:
            self.enemy = enemies.Ogre()
            self.alive_text = "An ogre is blocking your path!"
            self.dead_text = "A dead ogre reminds you of your triumph."
            
        elif r < 0.95:
            self.enemy = enemies.BatColony()
            self.alive_text = "You hear a squeaking noise growing louder...suddenly you are lost in a swarm of bats!"
            self.dead_text = "Dozens of dead bats are scattered on the ground."
            
        else:
            self.enemy = enemies.RockMonster()
            self.alive_text = "You've disturbed a rock monster from it's slumber!"
            self.dead_text = "Defeated, the monster has reverted into an ordinary rock."
            
        super().__init__(x, y)
    
    def intro_text(self):
        text = self.alive_text 
        if self.enemy.is_alive() else self.dead_text
        return text

    def modify_player(self, player):
       if self.enemy.is_alive():
           player.hp = player.hp - self.enemy.damage
           print("Enemy does {} damage. You have {} HP remaining.".
                 format(self.enemy.damage, player.hp))

This is the portion that is giving me the error.

def intro_text(self):
        text = self.alive_text 
        if self.enemy.is_alive() else self.dead_text
        return text

This is the error message.

File "/home/myron/Desktop/Gateway/world.py", line 50
   if self.enemy.is_alive() else self.dead_text
                            ^
SyntaxError: invalid syntax

Try something like this (untested):

def intro_text(self):
        text = self.alive_text if self.enemy.is_alive() else self.dead_text
        return text

I’m guessing it’s intended to be a ternary expression.

Thank you! This appears to have worked.

To elaborate further, Python statements end at the newline unless there
is an open syntactic feature such as brackets or a multiline string
(''').

So you can break a ternary expression (or other long expression) aacross
multiple lines for clarity like this:

 text = (
     self.alive_text
     if self.enemy.is_alive()
     else self.dead_text
 )

Cheers,
Cameron Simpson cs@cskk.id.au

I appreciate the extra info. This helps me to better understand what I am doing.