Search code examples
pythonsys

Doing print("Enter text: ", end="") sys.stdin.readline() does not work properly in Python-3


Something weird happened when i did this

import sys

print("Enter text: ", end="")
sys.stdin.readline()

Output:

<input>
Enter text: 

(Here, <input> (excluding quotation marks means that input happened on that line))

Why does the code run as though my code was

import sys

sys.stdin.readline()
print("Enter text: ", end="")

(Debian/Ubuntu, Python-3.8.10)

Any reason(s) for this?


Solution

  • I think, that stdout hasn't flushed before you enter your input.

    Try this

    import sys
    
    print("Enter text: ", end="")
    sys.stdout.flush()
    sys.stdin.readline()
    
    > Enter text: 123