Q:

Calculating Compressibility factor from the Van der Waals' Gas equation

So this problem has been bugging me for a long time. According to Wikipedia the compressibility factor $Z$ is defined as the ratio of the volume occupied by a real gas divided by the volume occupied by an ideal gas at the same temperature and pressure. 
$$Z = \frac{V_\text{real}}{V_\text{ideal}}$$
Now the ideal gas equation is 
$$P_\text{ideal} V_\text{ideal} = nRT$$
Now from here we can substitute the value of $V_\text{ideal}$ into the $Z$ expression to get $$Z = \frac{P V_\pu{m}}{RT}$$
Where $V_\pu{m}$  is the molar volume of the gas and $P$ is the value of $P_\pu{ideal}$ that was in the ideal gas equation.
OK so far so good. 
Deriving the compressibility factor value from the van der Waals' gas equation:
We know for real gases the pressure exerted by them is always less than the pressure exerted by an ideal gas so we correct the pressure by the term 
$an^2/V^2$
$$P_{\text{ideal}}= P_{\text{real}} + \frac{an^2}{V^2}$$
And the volume occupied by the gas molecules can be written as
$$ V_{\text{ideal}} = V_{\text{real}} - nb$$
On substituting the above values of $P_\text{ideal}$ and $V_\text{ideal}$ in the ideal gas equation, we get our van der Waals' Gas equation
$$\left(P+\frac{an^2}{V^2}\right)(V-nb) = nRT$$
Please note that in the above equation $P$ is $P_\text{real}$ and not $P_\text{ideal}$. Now I'm going a step further to calculate the value of $Z$ from this equation by shifting the terms and I got this $$Z = \frac{P V_\pu{m}}{RT} = \frac{V_\pu{m}}{V_\pu{m} - b} - \frac{a}{RTV_\pu{m}}$$
Now here's the problem that I'm facing: In the above expression the value of $P$ is not the $P_\text{ideal}$ but it is the value of $P_\pu{real}$ that we had in the van der Waals' gas equation. Doesn't that contradict the whole definition of Compressibility factor that we had learned in the first place? How this expression for $Z$ can be correct?
Thanks for reading. 

A:

$$Z = \frac{V^{\mathrm{r}}_{\mathrm{m}}}{V^{\circ}_{\mathrm{m}}}\tag{1}\label{1}$$
$V^{\mathrm{r}}_{\mathrm{m}} = \text{Volume of 1 mol real gas.}$
$V^{\circ}_{\mathrm{m}} = \text{Volume of 1 mol perfect gas.}$
The van der Waals equation is $$ \left( P^{\text{r}} + \frac{an^2}{(V^{\text{r}})^2}\right) (V^{\text{r}} - nb ) = nRT \tag2\label{2}$$
Now, consider you have a container containing a $1~\mathrm{mol}$ real gas. You know its pressure $P^{\text{r}}$, volume $V^{\text{r}}$ and temperature $T$ and you wish to find the compressibility factor of this gas. 
So, for calculating $Z$, we know the real volume of the gas, i.e $V^{\mathrm{r}}$ and now we need to calculate  $V^{\circ}$ which is the volume it should have occupied if it behaved like a perfect gas, i.e. it obeyed the perfect gas law.
Thus, $$V^{\circ} = \frac{RT}{P^{\text{r}}}$$
Therefore, substituting this value of $V^{\circ}$ in $\eqref{1}$ $$Z = \frac{V^{\mathrm{r}} P^{\text{r}}}{RT}\tag3\label{3}$$
From $\eqref{2}$, by rearranging the terms, we get $$P^{\text{r}} = \frac{RT}{(V^{\text{r}} - b)} - \frac{a}{(V^{\text{r}})^2} \tag4\label{4}$$
Substituting this value of $P^{\text{r}}$ in $\eqref{3}$, we get $$Z = \frac{V^{\text{r}}}{(V^{\text{r}} - b)} - \frac{a}{V^{\text{r}}(RT)} \tag5\label{5}$$

