Motivation. I recently published an introductory programming textbook that follows a language-agnostic approach. Although the notation I use is a tiny subset of Java, the book's conceit is that it is essentially a universal subset of all imperative programming languages, and an Appendix shows the minor changes needed to map the examples into (say) Python, JavaScript, or C/C++.
The book makes extensive use of indentation to show the refinement hierarchy in code, where comments are used for specifications. For example, Chapter 1 contains this example:
/* Output the Integer Square Root of an integer input. */
/* Obtain an integer n≥0 from the user. */
int n = in.nextInt();
/* Given n≥0, output the Integer Square Root of n. */
/* Let r be the integer part of the square root of n≥0. */
int r = 0;
while ( (r+1)*(r+1) <= n ) r++;
System.out.println( r );
Connundrum. What I overlooked until just now is that there appears to be a fundamental incompatibility between my use of indentation to show the refinement hierarchy (as illustrated above) and Python's use of indentation as syntax. In particular, my (working) Python version of the sample code is:
# Output the Integer Square Root of an integer input.
# Obtain an integer n≥0 from the user.
n = int(input())
# Given n≥0, output the Integer Square Root of n.
# Let r be the integer part of the square root of n≥0.
r = 0
while ( (r+1)*(r+1) <= n ):
r = r + 1
print( r )
The problem is that there would seem to be no way in Python to indent code (say, to be consistent with the refinement hierarchy) and have that indentation NOT be interpreted as syntax! The use of indentation in my book to show refinement is pervasive, so unless I can find a solution, I would appear to be hosed.
Seeking a solution. What I would seem to need is a Unicode space character that can be used to "physically" create whitespace at the beginning of a line but that is otherwise ignored by the Python compiler (say) for the purpose of determining "logical" indentation, or as a character in a subsequent identifier. So far, I haven't found such a character.
Does anyone have a suggestion?
I tried using an empty character (as per emptycharacter.com) before "n=int(input())". It was ignored as a "logical" indentation, was displayed as a red dot in the editor, but was then flagged by the compiler as an "invalid character in identifier".