Search code examples
pythonindentationdefinition

why Python isn't completing the def statement?


Still beginning my journey using Python so this is a simple question that I don't understand.

Trying to use Ziggy's definition statement of Cramer's V statistic from here: Using pandas, calculate Cramér's coefficient matrix

but when I put it into Python the definition doesn't end at the return:

>>> import pandas as pd
>>> def cramers_corrected_stat(confusion_matrix):
...     # calculate Cramers V statistic for categorial-categorial association.
...     # uses correction from Bergsma and Wicher,
...     # Journal of the Korean Statistical Society 42 (2013): 323-328
...
...     chi2 = ss.chi2_contingency(confusion_matrix)[0]
...     n = confusion_matrix.sum()
...     phi2 = chi2/n
...     r,k = confusion_matrix.shape
...     phi2corr = max(0, phi2 - ((k-1)*(r-1))/(n-1))
...     rcorr = r - ((r-1)**2)/(n-1)
...     kcorr = k - ((k-1)**2)/(n-1)
...     return np.sqrt(phi2corr / min( (kcorr-1), (rcorr-1)))
...

What am I not seeing?


Solution

  • When you're in REPL, you need two consecutive blank lines to finish the statement. However writing code in REPL is quite cumbersome, as you can't easily edit/fix previously written code, so I'd suggest you to save anything you do into .py file, where you can edit the code more easily and then run the code via python ./myfile.py.