-2

I am really new on python and try to find a good IDE My friend suggest me sublime text3.

But when I try to write some code, it come up a question:

I try to write a code that will read the line from user input.

import sys    
a = sys.stdin.readline()    
print (a)

At beginning I expect some cmd jump out and let me type something to let my program read. But there's nothing happen....

Can someone tell me that can SublimeText read user input? or did I do something wrong....

(I find out that there is a post discuss about 'Using sys.stdin.readline() to read multiple lines from cmd in Python'

Using sys.stdin.readline() to read multiple lines from cmd in Python

but I'm not sure if this is the case for sublime.....)

Hi everyone again. Sorry for the miss leading title and thank you for answering my question again!

DYZ point out the question I would want to ask, and actually I also try package "SublimeREPL". However, it's not working :(( (the figure show it)

I also try my code on terminal and it works... Can some one tell me where did I did wrong? or maybe I shouldn't do this on Sublime..

thank you guys again and sorry for confusing!

enter image description here

Community
  • 1
  • 1
  • how do you start your python program? Do you do that in sublime or in the shell? – hansaplast Feb 05 '17 at 20:32
  • First, you probably should not use `sys.stdin.readline()`. Use `input()` instead. Second, the parameter to `input()` is the prompt that is displayed just before the program pauses and lets you enter the input. I understand that it's the missing prompt that are you looking for. And the title of your question is misleading, because you do not attempt to read multiple lines. – DYZ Feb 05 '17 at 20:35
  • Possible duplicate of [How do I run Python code from Sublime Text 2?](http://stackoverflow.com/questions/8551735/how-do-i-run-python-code-from-sublime-text-2) – mkrieger1 Feb 05 '17 at 22:03
  • Possible duplicate of [Trouble with User Inputs in Sublime Text](http://stackoverflow.com/questions/38176401/trouble-with-user-inputs-in-sublime-text) – Keith Hall Feb 06 '17 at 06:27

2 Answers2

0

Correct me if I misunderstand what you are trying to do, but to get user input in python theres no need for importing sys, just use raw_input(). For example:

x = raw_input("enter something: ")
print "you entered", x

This program (written in sublime by the way) when run in the command prompt, produced the following output:

python user-input.py 
enter something: hello
you entered hello

In this case I entered "hello". If you don't need a prompt, just don't pass any parameter to the raw_input method. Let me know if this wasn't what you needed or if you need any clarification.

Wso
  • 192
  • 2
  • 11
0

See this question on using stdin: How to finish sys.stdin.readlines() input?

Example below (From the Q):

>>> import sys
>>> message = sys.stdin.readlines()
Hello
World
My
Name
Is
James
Bond
# <ctrl-d> EOF sent
>>> print message
['Hello\n', 'World\n', 'My\n', 'Name\n', 'Is\n', 'James\n', 'Bond\n']

If using Python 2.x it is recommended you use the raw_input() function and the input() function in Python 3.x. Like below

>>> raw_input()
test
'test'
Community
  • 1
  • 1
ScottSmudger
  • 351
  • 2
  • 8
  • 1
    There's a difference between readline and readline*s*. His code should work fine. The only problem is probably that he doesn't know where to type his input. – zondo Feb 05 '17 at 21:28