top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Input without line break in Python, is it possible?

+1 vote

The source code:

for i in range(8):
 n = input()

When we run it, consider the numbers below is the user input,

(and so forth)

my question, can i make it in just a single line like,

1 2 3 (and so forth)
posted Dec 4, 2013 by Sheetal Chauhan

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button
raw_input() is going to give you a string that you can then parse any way you want.

1 Answer

+1 vote

Not easily while processing the input one at a time. You can, however, read one line of input and then split it:

 s = input()
 bits = s.split()
 if len(bits) != 8:
 for bit in bits:

You could make it a bit more robust with something like:

 answers = []
 while len(answers) < 8:
 s = input()
 del answers[8:] # we only want 8, so throw away extras
 for answer in answers:

which would at least ensure that you have 8 entries.

answer Dec 4, 2013 by Amit Parthsarthi
Similar Questions
+1 vote

I'm using Python 2.7 under Windows and am trying to run a command line program and process the programs output as it is running. A number of web searches have indicated that the following code would work.

import subprocess

p = subprocess.Popen("D:PythonPython27Scriptspip.exe list -o",
for line in p.stdout:
 print line

When I use this code I can see that the Popen works, any code between the Popen and the for will run straight away, but as soon as it gets to the for and tries to read p.stdout the code blocks until the command
line program completes, then all of the lines are returned. Does anyone know how to get the results of the program without it blocking?

0 votes

Both the functions are used to take user inputs but how to decide which one to use ?

0 votes

I am writing a command line tool in python to generate one time passwords/tokens. The command line tool will have certain sub-commands like --generate-token and --list-all-tokens for example. I want to restrict access to certain sub-commands. In this case, when user tries to generate a new token, I want him/her to authenticate against AD server first.

I have looked at python-ldap and I am even able to bind to the AD server. In my application I have a function

 def authenticate_user(username, password): pass

which gets username and plain-text password. How do I use the LDAPObject instance to validate these credentials?

+2 votes

I am looking for an interface that takes a string as argument. The string is to be treated as if it is a command line and transformed into an argv list.

"ls file" -> ['ls', 'file']
"ls *.py" -> ['ls', '', '', ...]
"ls '*.py'" -> ['ls', '*.py']

Does something like this already exist? I looked around but seem to find only things only partially do things like this, like shlex.split.