1

I'm writing a Python program (on OS X/Linux) in which a user has to type some words in a while loop. This is the relevant part of it:

i = 0
array = []
while (i < 10):
   array.append(raw_input("Prompt: "))
   i = i + 1

However I'd like to stop the input as soon as the user types a SPACE, without the need to press ENTER.

I know I can't do this using raw_input, however I did not find any way to achieve what I'm trying to do. Basically I would like the user to be able to type one word after the other, using the SPACE button instead of ENTER to input the next word.

Do you have any idea? Thanks in advance.

user2747949
  • 163
  • 1
  • 6
  • 13
  • then you'll have to use other input methods. raw_input waits until an eol/eod is encounted (e.g. user hits return). you need to process every key stroke and check for spaces. – Marc B Jul 13 '16 at 15:53
  • It's not possible with `raw_input`, `input` or `sys.stdin.read(1)` either. There is no generic solution which works on every operating system. – pts Jul 13 '16 at 15:53
  • This is something pretty much platform-specific. [see here](http://stackoverflow.com/questions/510357/python-read-a-single-character-from-the-user) -- You could also consider using something like tkinter for handling events like that or remapping the Enter key to spacebar during the input process -- but probably more involved than you're looking for and you'd (depending) want/need to acocunt for various keyboard layouts. – sytech Jul 13 '16 at 15:58
  • http://stackoverflow.com/a/21659588/344286 Is really what you want – Wayne Werner Jul 13 '16 at 15:58

1 Answers1

1

It's not possible with raw_input, input or sys.stdin.read(1) either. There is no generic solution which works on every operating system.

Here is how to read a single character from the user in Python, without pressing Enter: Python read a single character from the user

Then you need to process each character in a for loop.

Community
  • 1
  • 1
pts
  • 80,836
  • 20
  • 110
  • 183