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.