I am currently retesting a script directly from the CMD line. Note that when I run it using IDLE, the outputs are as expected. Not so from the CMD line.
The test script is:
# teststreams.py
def interact():
print('Hello stream world') # print sends to sys.stdout
while True:
try:
reply = input('Enter a number>') # input reads sys.stdin
num = int(reply)
print("%d squared is %d" % (num, num ** 2))
except EOFError:
break # raises an except on eof
except ValueError:
break
print('Bye')
if __name__ == '__main__':
interact() # when run, not imported
Here is how I tested the script from the command line:
C:\Desktop\Temp_Directory>py teststreams.py
pre if
Hello stream world
pre reply
Enter a number>post reply
8 squared is 64
pre reply
Enter a number>post reply
6 squared is 36
pre reply
Enter a number>Bye
post if
Before, I used to get the following (the correct expected output as opposed to the output above):
Hello stream world
Enter a number>12
12 squared is 144
Enter a number>10
10 squared is 100
Enter a number>^Z
Bye
It appears that it is unexpectedly adding: pre if, pre reply, post reply, and post if.
Then you weren’t running the code you thought you were. There are a few possible reasons for this, but the most obvious ones (file name confusion, different directories with the same named file, etc) have already been covered, so I’ll just mention this one: Python retains a precompiled copy of scripts that you import, and can run those without needing to look at the original .py file. Normally, there’s a check to make sure the file hasn’t changed; but it is possible that something went wrong. (This is Windows. I’ve seen much MUCH weirder things. [1]) Check to see if there’s a file called teststreams.pyc or similar, possibly in a subdirectory called __pycache__.