TITLE: Continuous Functions from $\mathbb{R}$ to $\mathbb{Q}$
QUESTION [31 upvotes]: The following is not a homework problem. I am doing it for self study. 

Prove that any continuous function from $\mathbb{R}$ to $\mathbb{Q}$ is constant. 

Here is my proof:

Let $f:\mathbb{R}\rightarrow \mathbb{Q}$ be such a function. 

? We first show that $\mathbb{Q}$ is disconnected. Let $p$ be an irrational number. Then we can write $\mathbb{Q}$ as 
$$ \big( \mathbb{Q} \cap (-\infty,p) \big) \cup \big( \mathbb{Q} \cap (p,\infty) \big),  $$ 
which is a disconnection of $\mathbb{Q}$. 

Moreover, $\mathbb{Q}$ is also totally disconnected, as any open subset $U$ of $\mathbb{Q}$ must contain two rational numbers and there is always an irrational number between the two, and we can use that rational number to create a disconnection. Therefore the only connected subsets of $\mathbb{Q}$ are the singleton sets $\{q\}$, where $q \in \mathbb{Q}$, which are closed. 
We will show that $f(\mathbb{R})$ must be connected. Assume not, then 
  $$ f(\mathbb{R})= U\cup V, $$
  where $U$ and $V$ are nonempty open subsets of $\mathbb{Q}$ such that 
  $$ U \cap V= \emptyset .$$ 
  This implies
  $$
\mathbb{R} = f^{-1} \big( U \cup V \big) = f^{-1}(U)\cup f^{-1}(V), 
$$
  where $f^{-1}(U),f^{-1}(V)$ are nonempty, nonintersecting subsets of $\mathbb{R}$. This, however, is a contradiction, as $\mathbb{R}$ is connected. Therefore no such set $U$ and $V$ can exist. 
As we have already shown, the only connected subsets of $\mathbb{Q}$ are the singleton sets $\{q\}$, so $f(\mathbb{R})$ must be such a set.

Something about this does not seem quite satisfactory, as if I am missing something. Could anyone tell me a flaw in my logic? Also, is there a more satisfactory way to prove this theorem?

REPLY [11 votes]: Suppose you know that

Every continuous function from $\mathbb{R}$ to $\mathbb{Q}$ is a continuous function from $\mathbb{R}$ to $\mathbb{R}$.
The intermediate value theorem holds.
The irrationals are dense in $\mathbb{R}$.

Then a nonconstant continuous function would have to pass through the irrational numbers between any two of its values.
So then I suppose I would ask which of the three bulleted points were already established and which to prove as lemmas.
Nobody should ever write "Prove that any continuous function from whatever to whatever is constant." That could be understood as "Pick any continuous function from whatever to whatever and prove that it's constant", but what is more likely intended is "Prove that every continuous function from whatever to whatever is constant." Merely changing "any" to "every" eliminates all ambiguity.