1

I am on Python 3.5 inside Spyder 3.1.3. When I enter the following code, x has an empty string. I am not asked for any user input. Cursor directly goes to the next line

import sys

x = sys.stdin.read()

print(x)

So after executing this code, x = '' So what is happening here ?

2 Answers 2

2

When the cursor jumps to the newline, it has started to read from stdin, so anything you type would be passed to variable x and then re-printed once you break out from stdin.

If you want user input you can use:

x = input('Say something: ')
Sign up to request clarification or add additional context in comments.

3 Comments

actually, when I run the program in the command prompt in windows, it does print the the value of x, but inside the Spyder console, the cursor just goes to the next line without waiting for the input. and if I type x there , it will just print an empty string
so it seems that the Spyder console does not behave like a system terminal. How can I have Spyder console behave like system terminal in windows ?
Sorry I don't know Spyder
2

The stdin and stdout are references to file descriptors for the host OS. You didn't state the system you were running on, however in Linux; any process that is a child of the initializing process would share the same stdin / stdout.

As an example, when running from the command line or terminal, you can pipe information into it.

echo "Say something: " | ./yourscript.py

yourscript.py

#!/usr/bin/python

import sys

for line in sys.stdin.readlines():
    print('boom', line)

1 Comment

so is Spyder console different from command prompt on windows ?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.