To show a morphism of affine k-varieties which is surjective on closed points is surjective This is a exercise from Ravi Vakil's Foundations of Algebraic Geometry, Ex 7.4.E.

Assume Chevalley's theorem. Show that a morphism of affine $k$-varieties $\pi:X \rightarrow Y$ is surjective iff it is surjective on closed points (i.e. if every closed point of $Y$ is the image of a closed point of $X$).

I want to show 'if' part. Since $X,Y$ are both $k$-varieties so $\pi$ is morphism of Noetherian scheme. By Chevalley's thm, $\pi(X)=\cup_{i=1}^{N}O_i\cap C_i$, where $O_i$ are open, $C_i$ are closed and $\{O_i \cap C_i\}$ are disjoint. Since $\pi$ is surjective on closed points, we know $\pi(X)$ is dense. If I can prove $\pi(X)$ is open, then I am done. Can anyone provide some hints for me?
 A: Here's a slightly different take:
Suppose that the map is not surjective, then take $p \notin \operatorname{im}(\pi)$. By Chevally's theorem, the image is constructible, whence the complement is as well, so is a disjoint union of locally closed sets $U_i\cap Z_i$ where $U_i$ is open and $Z_i$ is closed, say $p \in U_1\cap Z_1$. The closed points of $Z_1$ are dense*, so $U_1\cap Z_1$ contains a closed point. But this is impossible, since $U_1\cap Z_1$ was part of the complement of the image, and the image contains all the closed points.
*Recall that the closed points of $\operatorname{Spec}(A)$ for $A$ a finitely generated $k-$algebra are dense, and thatif $V(I)$ is a closed subset, it is homeomorhpic to $\operatorname{Spec}(A/I)$, where $A/I$ is also a finitely generated $k-$algebra. This is enough for the task at hand, but it is useful to observe that if $X$ is scheme locally of finite type over a field $k$, and $Z\subset X$ is closed, the closed points of $Z$ are dense in $Z$.
A: Let $q \in Y$. It suffices to show that the generic point of $V(q)$ is in the image of $\pi^{-1}(V(q)) \to V(q)$ (note that $\pi^{-1}(V(q)) \ne \emptyset$, since $V(q)$ contains a closed point). Thus we reduce to the case $\pi : X \to Y$ where $Y$ is irreducible with generic point $q$, and we wish to show $q \in \text{im}(\pi)$.
Since $\text{im}(\pi)$ is constructible, so is $Y \setminus \text{im}(\pi)$, say $Y \setminus \text{im}(\pi) = \bigsqcup_{i=1}^n (U_i \cap Z_i)$ with $U_i$ open, $Z_i$ closed. If $q \in Y \setminus \text{im}(\pi)$, say $q \in U_1 \cap Z_1$, then $Z_1 = Y$ (since $q \in Z_1 \implies Z_1$ dense), so in fact $q \in U_1$, and $U_1 \subseteq Y \setminus \text{im}(\pi)$, which implies $\text{im}(\pi) \subseteq Y \setminus U_1 \ne Y$. 
Now, if $S$ is the coordinate ring of $Y$, then $Y \setminus U_1 = V(I)$ for some $S$-ideal $I$. Then $V(I)$ contains every closed point of $Y$, i.e. $\displaystyle I \subseteq \bigcap_{m \in \text{mSpec}(S)} m = 0$ (as $S$ is a finitely generated reduced $k$-algebra). But this means $V(I) = V(0) = Y$, contradicting $q \in U_1$.
