def get_TD_error(new_value, value, reward, has_finished, discount):
    """Return TD error.

    Args:
        new_value: Value at next state.
        value: Value at current state.
        reward: Reward in transitioning to next state.
        has_finished: If the game has finished.
        discount: Discount factor.

    Returns:
        The TD error.
    """
    if has_finished:
        # when the game has been won, the reward itself is the total return, shouldn't bootstrap anymore.
        return reward - value
    else:
        return reward + discount * new_value - value