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.