Python time.sleep() not sleeping
Question: I'm coding a game, and the fight function seems to be tripping me up. Here's
the combat snippet of my code:

    
    
    def combat(player, enemy, dun):
      print("\n"*100 + "A " + enemy.name + " has attacked you!")
      while player.health > 0 and enemy.health > 0:
        print(enemy.name[:1].upper() + enemy.name[1:], "health:", enemy.health)
        print("Your health:", player.health)
        cmd = input(">")
        if cmd == "attack":
          enemy.health -= player.atk(enemy)
        if cmd == "run":
          coin = random.choice(["heads", "tails"])
          if coin == "heads":
            break
          else:
            print("You couldn't escape.")
        if cmd == "equip":
          target = input("Which item?\n>")
          print(player.equip(target))
        player.health -= enemy.atk(player)
    
      if enemy.health <= 0:
        print("You defeated the", enemy.name + "!")
        if enemy.drop != None:
          return "Enemy defeated.\nThe " + enemy.name + " dropped a " + enemy.drop + "!"
          dun.data[dun.pos][2].append(enemy.drop)
        else:
          return "Enemy defeated."
        time.sleep(1.5)
        out = 1
      elif player.health <= 0:
        print("You died fighting %s..." % enemy.name)
        dun.pos == (0, 0)
        player.inventory == []
        return "You reawaken in the same room you started in, pack empty..."
        time.sleep(3)
    

I have imported the random module and the time module, it's just not in the
snippet.

When the sequence ends, it does not sleep and goes right away into the main
game loop (which I can give if needed).

Apart from being messy, what am I doing wrong?


Answer: The `return` statement immediately exits your function. Any code after it
_will not run_. You can `sleep` before your `return`:

example:

    
    
    time.sleep(1.5)
    return "Enemy defeated."
    

But it probably makes a lot more sense to just sleep after you call your
function:

    
    
    combat(...)
    time.sleep(5)
    

