TITLE: Locally Constant Functions on Connected Spaces are Constant
QUESTION [38 upvotes]: I am trying to show that a function that is locally constant on a connected space is, in fact, constant. I have looked at this related question but my approach is a little different than the suggested approach and I'm unsure about the final step and would appreciate a tip. Here is what I have so far:
Let $f$ be a locally constant function on the connected space $U$. Assume that $f$ is not constant. Then, there are distinct points $x$ and $y$ such that $f(x) \neq f(y)$. Now, since $f$ is locally constant there are neighborhoods $V_x$ of $x$ and $V_y$ of $y$ such that
$$
f(V_x) = k_x, \;\; f(V_y) = k_y
$$
for some constants $k_x \neq k_y$ .It follows that $V_x \cap V_y =\emptyset$
Now, let $A = U-V_x \cup V_y$ and $B = V_x \cup V_y$ so that $U = A \cup B$. With this, we have expressed $U$ as a union of disjoint sets. Since $V_x$ and $V_y$ are open $B$ is open. Note that if $A$ is empty we are done because $V_x$ and $V_y$ would comprise a separation of $U$ wich would imply that the assumption about f being not constant was faulty. So, assume $A$ is nonempty.
At this point, I want to show that $A$ itself is open. If I can do this, I believe the proof will be complete. One way I've thought about doing this is to choose a neigborhood of some point $a \in A$ and if it is not already disjoint from $B$, shrink it until it is. This would then demonstrate that $A$ is open. 
Am I on the right track here?

REPLY [8 votes]: Are we don't need the hypothesis $f$ is continuous?
Since if we pick $z\in X$ then $A = \{x\in X: f(x) = f(z)\}$ is open, to see this, let $y\in A$, then by $f$ is locally constant, exist open set $B_y$ containing $y$ such that $f(B_y) =\{f(y)\} =\{f(z)\}$ so $B_y \subset A$. Otherwise, $X\backslash A = \{x\in X: f(x)\neq f(z)\}$ is also open, let $w\in X\backslash A$, then exist open set $B_w$ containing $w$ such that $f(B_w) = \{f(w)\}$, so $B_w \subset X\backslash A$.\
Hence $X = A\cup (X\backslash A)$ where $A$ and $X\backslash A$ are open, by connectedness we have $X = A$ since $A\neq \emptyset$