Prove that if $X$ and $Y$ are metric spaces with locally constant function $f\colon X \to Y$ and $X$ is connected, then $f$ is constant. Suppose $(X, \rho)$, $(Y, \tau)$ are metric spaces and that $f\colon X \to Y$ is a continuous function. Suppose further that for each $x \in X$, there exists a neighborhood $N_x$ centered at $x$ such that $f(y) = f(x)$ for every $y \in N_x$ (in this sense $f$ is locally constant).
Show that if $X$ is connected, then $f$ is constant on all of $X$, i.e. there exists $y_0 \in Y$ such that $f(x)=y_0$ for all $x \in X$
My attempt:
Suppose that $f(X) = A \cup B$ is a separation. Let $C = f^{−1}(A)$, $D = f^{−1}(B)$. Then $C, D \neq \emptyset$ and $C\cap D = \emptyset$.
Write $A = f(X) \cap U$, where $U \subseteq Y$ is open. Then $C = f^{−1}(A) = f^{−1}(U)$ is open since $f$ is continuous.
Similarly, $D$ is open.
I proved $F(x)$ is continuous but still I don't have any idea to proceed to what I want. Any help would be appreciated.
 A: The function is continuous, so the preimage of a value is closed. By hypothesis, the preimage of a value is open. so if $f(x_0) = c,$ then $f^{-1}(c)$ is open and closed, thus a connected component of $X.$
A: In the non-trivial case where $X$ is not empty, take $x\in X$ and let $A=f^{-1}\{f(x)\}.$
Each $y\in A$ has an open nbhd $U(y)$ on which $f$ is constantly equal to $f(y),$ and  $f(y)=f(x).$ So $U(y)\subset A.$ So $A$ is open.
Any $z\in \overline A$ has an open nbhd $V(z)$ on which $f$ is constant. And $V(z)$ contains some $y\in A$ because $z\in \overline A,$  so $f(z)=f(y)=f(x),$ so $z\in A.$ So $A$ is closed.
$A$ is open and closed and not empty, and $X$ is connected , so $A=X.$
BTW. There are many consequences of continuity that are equivalent to continuity. Some of them are more useful   than others for some problems. A function $f$ is locally  continuous iff every point in its domain has a nbhd on which $f$ is continuous.  A function is    continuous iff it is locally continuous. In particular, a locally constant function is continuous.
