Input without line break, is it possible?

G

geezle86

The source code:

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

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

1
2
3
4
5
6
(and so forth)

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

1 2 3 4 5 6 (and so forth)

Can I?
 
T

Tim Chase

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

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

1
2
3
4
5
6
(and so forth)

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

1 2 3 4 5 6 (and so forth)

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:
what_now("?")
else:
for bit in bits:
do_something(bit)

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

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

which would at least ensure that you have 8 entries.

-tkc
 
M

Mark Lawrence

The source code:

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

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

1
2
3
4
5
6
(and so forth)

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

1 2 3 4 5 6 (and so forth)

Can I?

Yes you can get them on a single line, see the response from Tim Chase.
But just to be crystal clear, are you aware that you're getting string
representations of numbers, and not the numbers themselves?
 
T

Tim Chase

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

answers = []
while len(answers) < 8:
s = input()
answers.append(s.split())

this should be

answers.extend(s.split())

instead of .append()

That's what I get for coding in my inbox rather than copy/pasting
from tested Python code.

-tkc
 
C

Chris Angelico

Yes you can get them on a single line, see the response from Tim Chase. But
just to be crystal clear, are you aware that you're getting string
representations of numbers, and not the numbers themselves?

Just to clarify, this is assuming that you're using Python 3. Geezle,
if you're using Python 2, you need to not use input() for anything -
use raw_input() instead, which will do what we're describing here.

I yearn for the day when nobody uses Python 2 any more so this doesn't
need to be asked.

ChrisA
 
M

Michael Torrie

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

1 2 3 4 5 6 (and so forth)

Can I?

Yes of course. raw_input() is going to give you a string that you can
then parse any way you want.
 
M

Mark Lawrence

Just to clarify, this is assuming that you're using Python 3. Geezle,
if you're using Python 2, you need to not use input() for anything -
use raw_input() instead, which will do what we're describing here.

Good point, I saw input() and automatically assumed Python 3, what a
sin! The assumption obviously, not Python 3!!
I yearn for the day when nobody uses Python 2 any more so this doesn't
need to be asked.

I'm contemplating what it would be like migrating code from Python 1.x
to Python 4.0, the fun and games that could be :)
 
C

Chris Angelico

I'm contemplating what it would be like migrating code from Python 1.x to
Python 4.0, the fun and games that could be :)

I never used Python 1.x seriously, but when I went digging in one of
my OS/2 machines a while ago, I found several Pythons, including a
1.something. Fortunately for my task at hand, there was also a 2.5 or
2.6 or somesuch, which served my purposes :)

ChrisA
 
M

Mark Lawrence

Yes of course. raw_input() is going to give you a string that you can
then parse any way you want.

That's it lad, you're in detention for one hour. You will write
repeatedly "I should be selling Python 3 instead of Python 2" :)
 
M

Michael Torrie

That's it lad, you're in detention for one hour. You will write
repeatedly "I should be selling Python 3 instead of Python 2" :)

Yup. Though if he is using Python 2, then input() is a real no-no.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top